Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

String retrieve question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jason Huang - 05 Sep 2007 03:48 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.