split method

  • Thread starter Thread starter Alberto
  • Start date Start date
A

Alberto

I have to store in a string array all the words of a text file. Actually I'm
doing this:

string [] v = text.Split(new char[] { ' ', '\n', '\t', '\r'})

but it doesn't work fine. When a new line is found the split method returns
two spaces.

How can I do fix it?
Thank you
 
Hello,
This is because you are splitting on '\n' and '\r' both. When you
analyze the string you'll find out that it contains \r\n for each line
break. A solution can be that you remove either '\n' from the string
first and then split on '\r' or vice versa.

Example:
text = text.Replace("\r\n","\n");
now, split the string for \n only.

HTH. Cheers :)
Maqsood Ahmed - MCAD.net
Kolachi Advanced Technologies
http://www.kolachi.net
 
Assuming that you're not talking about Unix, the line break is made up of
not one character, but 2: \r\n.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
If you are using, or can switch to .Net 2.0, you can use:

string [] v = text.Split(new char[] { ' ', '\n', '\t', '\r'},
StringSplitOptions.RemoveEmptyEntries)
 
Back
Top