By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,890 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,890 IT Pros & Developers. It's quick & easy.

Splitting with multiple spaces

P: n/a
I have a text file that appears to be delimited by multiple spaces. The
split function will only work with one space if I am correct. Is there some
way to split this file into an array without getting the extra spaces?
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Christine!

if understood you're needs correctly, i suggest you to use
System.Text.Regex's Split() function instead of the String's one. It
supports a String parameter.

--
Best Regards
Yanick
"Christine" <Ch*******@discussions.microsoft.com> a écrit dans le message de
news:9B**********************************@microsof t.com...
I have a text file that appears to be delimited by multiple spaces. The
split function will only work with one space if I am correct. Is there some way to split this file into an array without getting the extra spaces?

Nov 16 '05 #2

P: n/a
On Tue, 21 Dec 2004 12:07:02 -0800, Christine wrote:
I have a text file that appears to be delimited by multiple spaces. The
split function will only work with one space if I am correct. Is there some
way to split this file into an array without getting the extra spaces?


Use the Regex (System.Text.RegularExpressions.Regex) split instead as it
allows pattern matching rather than single character or specific string
matching.

Regex r = new Regex(" +");
string [] splitString = r.Split(stringWithMultipleSpaces);
--
Tom Porterfield
Nov 16 '05 #3

P: n/a
Okay that helps with the spaces, but now I have a problem of a carriage
return at the end of each line that Regex doesn't take care of. Is there
some way to take care of this as well as the multiple spaces? Thanks so much
for the help!

"Tom Porterfield" wrote:
On Tue, 21 Dec 2004 12:07:02 -0800, Christine wrote:
I have a text file that appears to be delimited by multiple spaces. The
split function will only work with one space if I am correct. Is there some
way to split this file into an array without getting the extra spaces?


Use the Regex (System.Text.RegularExpressions.Regex) split instead as it
allows pattern matching rather than single character or specific string
matching.

Regex r = new Regex(" +");
string [] splitString = r.Split(stringWithMultipleSpaces);
--
Tom Porterfield

Nov 16 '05 #4

P: n/a
On Wed, 22 Dec 2004 06:45:05 -0800, Christine wrote:
Okay that helps with the spaces, but now I have a problem of a carriage
return at the end of each line that Regex doesn't take care of. Is there
some way to take care of this as well as the multiple spaces? Thanks so much
for the help!


You have several options. You could use the System.String.Replace method
to replace the carriage return with an empty string, or you could use
System.String.TrimEnd - Ex:

string newString =
stringWithCarriageReturn.TrimEnd(new char[]{'\r','\n',' '});

Note that the above will also trim any trailing spaces.
--
Tom Porterfield
Nov 16 '05 #5

P: n/a
Thank you so much, that was very helpful!

"Tom Porterfield" wrote:
On Wed, 22 Dec 2004 06:45:05 -0800, Christine wrote:
Okay that helps with the spaces, but now I have a problem of a carriage
return at the end of each line that Regex doesn't take care of. Is there
some way to take care of this as well as the multiple spaces? Thanks so much
for the help!


You have several options. You could use the System.String.Replace method
to replace the carriage return with an empty string, or you could use
System.String.TrimEnd - Ex:

string newString =
stringWithCarriageReturn.TrimEnd(new char[]{'\r','\n',' '});

Note that the above will also trim any trailing spaces.
--
Tom Porterfield

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.