Results 1 to 6 of 6

Thread: I want to replace first colon in C# program

  1. #1
    Join Date
    Nov 2009
    Posts
    131

    I want to replace first colon in C# program

    Hello to all,
    I am new to this forum. I recently started learning java language. I want to replace first occurrence of a regular expression in a string in my c# program.
    For Eg.
    If string s1= ",a,b,c,d,e," then I want to do something like
    s1 = replacefirst(",","")
    to give following result
    s1 = "a,b,c,d,e"

    It means I want to replace first colon in C# program. Please help me to solve this problem.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: I want to replace first colon in C# program

    Hey it is very easy process. You have to use trim function to do this. Trim function is used to replace first colon in c# program. I have written following code using trim function trim function. Just use this code in your program to do this.

    var myTrimmedStringS1 = myStringS1.TrimStart(',');

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: I want to replace first colon in C# program

    You have to use following code in your program to replace first colon. It is very simple program. In the following program I have create object of Regex class to replace first colon and after that I have pass that object to another string to another string variable for storing. Just try to understand following code.


    string sa = "a;b;c;";
    Regex rx1 = new Regex(";");
    string sb = rx.Replace(ss, "", 1);

  4. #4
    Join Date
    Nov 2005
    Posts
    1,323

    Re: I want to replace first colon in C# program

    For replacing first colon in your code you have to use object of regex class. It is very simple. Rregex class is used to do such kind of operation. In Regex class you have to use "^" symbol, which is known as "beginning". It means you have to use following code in your program to do this.


    Regex.Replace(",a,b,c,d,e", "^,", "")

    After using this code you will get output like a,b,c,d,e

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Re: I want to replace first colon in C# program

    Instead of using any other function you can use simple If statement to replace first colon. It is very simple process. Just pass the string to Startswith( ) function for replacing first colon. After passing string then pass the value that you want to appear on first place. just take a look at following example.


    if(stringS1.Startswith(','))
    stringS2 =stringS1.Substring(1);

  6. #6
    Join Date
    May 2008
    Posts
    2,389

    Re: I want to replace first colon in C# program

    You can use index function to do this. I have written following code for you. In that code I have written index function to replace first colon. First I have save first colon of your string into index and I have replace that string with null value. Just try to understand following code.


    Code:
    int indexs = s1.IndexOf(",");
    s1 = index < 0 ? s1 : s1.Substring(0, indexs) + s1.Substring(indexs + 1);

Similar Threads

  1. Excel CSV Export use semi-colon as delimiter not comma
    By Celestial in forum MS Office Support
    Replies: 2
    Last Post: 17-01-2012, 01:22 PM
  2. Replies: 4
    Last Post: 21-05-2011, 10:23 AM
  3. Replies: 3
    Last Post: 28-01-2011, 01:57 PM
  4. Windows DEP program continuously closes the MSN mail program
    By Charioteer in forum Windows Software
    Replies: 4
    Last Post: 19-10-2010, 11:52 AM
  5. End Program ccSvcHst Program not responding
    By Leena in forum Operating Systems
    Replies: 6
    Last Post: 29-08-2008, 01:58 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,356,465.85660 seconds with 16 queries