String retrieve question

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

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
 
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
 
Jason Huang said:
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?
 
Alternatively, use a regular expression with a lookbehind prefix.
So if you want a digit sequence that comes after ABC, search for

(?<=ABC)\d+
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top