Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   replace first in c# (http://www.go4expert.com/forums/replace-c-sharp-t4871/)

rnbguy 25Jun2007 08:12

replace first in c#
 
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

shabbir 25Jun2007 10:59

Re: replace first in c#
 
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.

rnbguy 25Jun2007 11:40

Re: replace first in c#
 
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#"

shabbir 25Jun2007 13:42

Re: replace first in c#
 
use the substring function to get the part of the string.

yogeshsinghrana 15Jun2010 10:19

Re: replace first in c#
 
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());


All times are GMT +5.5. The time now is 06:17.