C#Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
I have an .aspx file with a string that is one line of data, this line has multiple instances of "ServiceName=" i'm working on a program that extracts each "ServiceName=" from the file, but using sting.INdexof only give me the first instance of "ServiceName=" how do iterate through all instances of "ServiceName=" when it is read as one line?
Are the occurrences of each item separated by something? If they are, you could split the string into an array, then parse each array item for the name(s) you are looking for.
Alternatively, you can use a Regular Expression to get the value, and it will return an matching groups. Regular Expressions sound a bit overkill, but in your example, no knowledge is required.
Code:
using System.Text.RegularExpressions;
string text = "ServiceName=";
MatchCollection matches = Regex.Match(sourceString, text, RegexOptions.SingleLine);
if(matches.Count==0)
//No matches found
else
{
foreach(Match m in matches)
{
//Match is contained at m.Value;
}
}
Thanks! this is very helpful, i want my program to be as robust as possible and the process you have outlined is much more efficent than the process I had come up with.