Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through indexof characters 3

Status
Not open for further replies.

HoustonGuy

Programmer
Jun 29, 2000
165
0
0
US
I'm new to C# - so I'm having trouble iterating through a string using indexof.

My String: "Hello|There|How|Are|You|"

I'm trying to loop through each found instance of the pipe character: "|" to provide a resultset like this:

Hello
There
How
Are
You

Simple and frustrating. But here is the last test code I have:

Code:
string s = "Hello|There|How|Are|You|";
int sLength = s.LastIndexOf("|");

while (s.IndexOf("|") <= s.Length)
            {
                int startblock = s.IndexOf("|") + 1;
                int endblock = s.IndexOf("|", startblock);

            MessageBox.Show(s.Substring(startblock, endblock - startblock));

            }

I've tried many different versions- but I can't find the correct code to loop through each iteration.
Thanks from a C# newbie.

Also - any site that has great string manipulation samples? I've found a few - but they never seem to show looping or conditional examples. :)
 
Why not use Split and loop through the array?
Code:
var arr = s.Split('|');
foreach(string chunk in arr)
{
   MessageBox.Show(chunk);
}

MakeItSo

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top