Problem:How to use the string array after using the Split() method?
I have a hashtable which stores all the lines of a paragraph at different indices.
Next,I want to split each line in the hashtable based on comma.
My Code:
foreach(DictionaryEntry entry in h1)
{
string val = (string)entry.Value;
string[] synonyms=val.Split(',');
string variable=synonyms[0];
/*works fine till here*/
int i=1
while(true)
{
Console.WriteLine(synonyms[i]);
i++;
}
}
What I want to do is once the line is split based on comma, each part is saved as an array..
In a string called variable, I am assigning part0..
Then I want to use a loop for parsing the parts from index 1 onwards..
Whenever I try to do that I get unhandled exceptions...
Re: Problem:How to use the string array after using the Split() method?
Quote:
Originally Posted by
Pearls
I have a hashtable which stores all the lines of a paragraph at different indices.
Next,I want to split each line in the hashtable based on comma.
My Code:[code]
foreach(DictionaryEntry entry in h1)
{
string val = (string)entry.Value;
string[] synonyms=val.Split(',');
string variable=synonyms[0];
/*works fine till here*/
int i=1
while(true)
{
Console.WriteLine(synonyms[i]);
i++;
}
}
What I want to do is once the line is split based on comma, each part is saved as an array..
In a string called variable, I am assigning part0..
Then I want to use a loop for parsing the parts from index 1 onwards..
Whenever I try to do that I get unhandled exceptions...
My 1st time..So having a little trouble in posting..
Re: Problem:How to use the string array after using the Split() method?
This index is zero based, if there is no 1, you will cause an exception.
You might also wish to check length as well: if synonyms[i].Length > 0 ...
Re: Problem:How to use the string array after using the Split() method?
ok..Thankx ZOverLord..I will do that as well..
I will explain with an example..
I have a line "Andy,Ram,Seth")
After splitting,I get
synonym[0]=Andy
synonym[1]=Ram
synonym[2]=Seth
I assign Andy to variable..
Next,I want to do the processing from index 1 i.e only for Ram and Seth..
Re: Problem:How to use the string array after using the Split() method?
Quote:
Originally Posted by
Pearls
ok..Thankx ZOverLord..I will do that as well..
I will explain with an example..
I have a line "Andy,Ram,Seth")
After splitting,I get
synonym[0]=Andy
synonym[1]=Ram
synonym[2]=Seth
I assign Andy to variable..
Next,I want to do the processing from index 1 i.e only for Ram and Seth..
Code:
string val = "Andy,Ram,Seth";
string variable = "";
String[] synonyms = val.Split(new Char[] { ',' });
if (synonyms.Length > 0)
variable = synonyms[0];
else
variable = "";
if ((synonyms.Length > 1)
&& (variable.Length > 0))
for (int i = 1; i < (synonyms.Length); i++)
{
// Your code here
if (synonyms[i].Length > 0)
MessageBox.Show(synonyms[i]);
}
else
{
// Was empty
}
IMHO, I would never assume a random string or a string array produced by a string split from a random string has any length, when doing processing like this, while some might say it is "OverKill" to check, **** Happens!