replace first in c#

rnbguy's Avatar
Light Poster
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
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
rnbguy's Avatar
Light Poster
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#"
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
use the substring function to get the part of the string.
0
yogeshsinghrana's Avatar, Join Date: Jun 2010
Newbie Member
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());