Hi,
In C#, how do I retrieve a special sub string within a big string?
e.g.,
the "...fdsajk...ABC123...fdfdskafdl... ..." string has 5000 characters,
and I want to retrieve the string "123" which is right after the "ABC",
given that counting the index of the "123" is nearly impossible.
Thanks for help.
Jason
Franck - 05 Sep 2007 04:22 GMT
the following code return a string of all character to the left of the
string your searching for. just check the lenght of it and thats it.
public static string LeftOf(string src, char c)
{
string ret=src;
int idx=src.IndexOf(c);
if (idx != -1)
{
ret=src.Substring(0, idx);
}
return ret;
}
*code taken from : http://www.codeproject.com/cs/library/stringhelpers.asp
Niels Ull - 05 Sep 2007 07:43 GMT
Alternatively, use a regular expression with a lookbehind prefix.
So if you want a digit sequence that comes after ABC, search for
(?<=ABC)\d+
Jon Skeet [C# MVP] - 05 Sep 2007 07:37 GMT
> In C#, how do I retrieve a special sub string within a big string?
> e.g.,
> the "...fdsajk...ABC123...fdfdskafdl... ..." string has 5000 characters,
> and I want to retrieve the string "123" which is right after the "ABC",
> given that counting the index of the "123" is nearly impossible.
> Thanks for help.
What do you mean by "counting the index of the 123 is nearly
impossible"? What's wrong with using IndexOf?

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too