replace first in c#

Discussion in 'C#' started by rnbguy, Jun 25, 2007.

  1. rnbguy

    rnbguy New Member

    Joined:
    Jun 25, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    im trying to find a way in c# to replace first occurance of a regular expression in a string.

    so if string = " found 1 found 2 found 3"

    i want to do something like :

    string = replacefirst("found","")

    to give results:

    string = " 1 found 2 found 3"

    thanx
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    As far as I can remember there is no direct function and so you need to be splitting it then replacing the string and then concatenate it back to get the complete string.
     
  3. rnbguy

    rnbguy New Member

    Joined:
    Jun 25, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    yeah i thought of that option too but the problem seems to be it wont let me split it on such a long combination of characters such as "i_love_c#"
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    use the substring function to get the part of the string.
     
  5. yogeshsinghrana

    yogeshsinghrana New Member

    Joined:
    Jun 15, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    this solution might help you

    string a = @"abc def ghijklmnopwxyzabcdefghijklmnop";
    a = System.Text.RegularExpressions.Regex.Replace(a, "^(a)b(c)", "yogesh");
    Response.Write(a.ToString());
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice