[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

string search...

Posted on 2007-10-09
5
Medium Priority
?
184 Views
Last Modified: 2010-04-15
Hi,
I need to get a SubString from a string but the SubString length can change and also the start position of the SubString can be changed.
The string that starts with "<User" and ends withe "</User>" (i need to get the string between them).
Any Idea?
Thanks
 
0
Comment
Question by:udir
  • 3
  • 2
5 Comments
 
LVL 22

Accepted Solution

by:
JimBrandley earned 2000 total points
ID: 20041451
You can do this:
int startPos = yourString.IndexOf(""<User");
int endPos = yourString.IndexOf("</User>" );
if ((startPos > -1) && (endPos > -1))
{
   startPos += 5;
   string middle = yourString.SubString( startPos, endPos - startPos);
}

Jim
0
 
LVL 1

Author Comment

by:udir
ID: 20041526
Hi,
Thanks, just for knoledge :
what is the meanning of :
startPos += 5;
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20041745
yourString.IndexOf("<User"); returns the start location of this string in the string you are searching. So, say the whole string is:

<User>Tom</User>

Then startPos is returned as zero. By adding 5 to startPos, we are preparing to get the substring that starts with ">".

BTW:

startPos += 5;

is a shorthad way of writing:
startPos = startPos + 5;

If you wanted to skip the ">" too, and just get "Tom", change the 5 to a 6.

Jim
0
 
LVL 1

Author Comment

by:udir
ID: 20041784
OK, Thanks a lot
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20041802
My pleasure. Good luck.

Jim
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question