472,119 Members | 1,850 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Normalizing strings + make array

I'd like a simple way of extracting everything that's not whitespace on each
line in a file. For example, if the program encounters this line

1 2\t\t3 \t\t4

I want a string array like this

{"1", "2", "3", "4"}

I tried working with String.Split() but found that it doesn't automatically
remove the empty elements in the resulting array. I haven't found any
normalize method in the API either, to replace the sequences of two or more
whitespace characters to a single space. Hope there's something I've
overlooked.

Thanks,

Gustaf

Nov 15 '05 #1
8 2328
Gustaf,

You might want to take a look at regular expressions for this. There
are escape sequences which can eliminate whitespace (I believe \w) and then
will split apart the string appropriately.

However, someone else in the group might be better suited to providing
the actual pattern. You will have to use the Regex class in the
System.Text.RegularExpressions namespace.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Gustaf Liljegren" <gu**************@bredband.net> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
I'd like a simple way of extracting everything that's not whitespace on each line in a file. For example, if the program encounters this line

1 2\t\t3 \t\t4

I want a string array like this

{"1", "2", "3", "4"}

I tried working with String.Split() but found that it doesn't automatically remove the empty elements in the resulting array. I haven't found any
normalize method in the API either, to replace the sequences of two or more whitespace characters to a single space. Hope there's something I've
overlooked.

Thanks,

Gustaf

Nov 15 '05 #2
Gustaf,

Check out System.Text.RegularExpressions.Regex. You probably want to match
against "\S*".

-Marc
"Gustaf Liljegren" <gu**************@bredband.net> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
I'd like a simple way of extracting everything that's not whitespace on each line in a file. For example, if the program encounters this line

1 2\t\t3 \t\t4

I want a string array like this

{"1", "2", "3", "4"}

I tried working with String.Split() but found that it doesn't automatically remove the empty elements in the resulting array. I haven't found any
normalize method in the API either, to replace the sequences of two or more whitespace characters to a single space. Hope there's something I've
overlooked.

Thanks,

Gustaf

Nov 15 '05 #3
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
You might want to take a look at regular expressions for this. There
are escape sequences which can eliminate whitespace (I believe \w) and then
will split apart the string appropriately.
\w is "word characters". \s is whitespace. (I say, after trying \w and
being surprised when it didn't work :)
However, someone else in the group might be better suited to providing
the actual pattern. You will have to use the Regex class in the
System.Text.RegularExpressions namespace.


I believe the pattern wanted here is @"\s+" with the string being
trimmed to start with.

Here's a sample program:

using System;
using System.Text.RegularExpressions;

class Test
{
static void Main()
{
string t = " 1 2\t\t3 \t\t4 ";

string[] x = Regex.Split (t.Trim(), @"\s+");
foreach (string s in x)
{
Console.WriteLine ("'{0}'", s);
}
}
}

That produces the output:

'1'
'2'
'3'
'4'

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Jon,

A more straightforward approach would be to use a matching regex, @"\S+",
non-whitespace words:

string thing = " 1 2\t\t3 \t\t4 john\ndoe h.e-r_e's s;o:m'e other words ";

MatchCollection words = Regex.Matches(thing, "\\S+");

foreach (Match m in words)

Console.WriteLine(m.Value);

-Marc

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
You might want to take a look at regular expressions for this. There are escape sequences which can eliminate whitespace (I believe \w) and then will split apart the string appropriately.


\w is "word characters". \s is whitespace. (I say, after trying \w and
being surprised when it didn't work :)
However, someone else in the group might be better suited to providing the actual pattern. You will have to use the Regex class in the
System.Text.RegularExpressions namespace.


I believe the pattern wanted here is @"\s+" with the string being
trimmed to start with.

Here's a sample program:

using System;
using System.Text.RegularExpressions;

class Test
{
static void Main()
{
string t = " 1 2\t\t3 \t\t4 ";

string[] x = Regex.Split (t.Trim(), @"\s+");
foreach (string s in x)
{
Console.WriteLine ("'{0}'", s);
}
}
}

That produces the output:

'1'
'2'
'3'
'4'

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #5
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote
Here's a sample program:
Thanks! Very helpful indeed.
string[] x = Regex.Split (t.Trim(), @"\s+");


Yes, this works fine.

But I just found another obstacle. The syntax in the input files allows
fields to be quoted if they contains spaces. So this

1 "2 3"\t\t4 \t\t5

should be splitted into

{"1", "2 3", "4", "5"}

This means whitespace should be removed only if it appears outside double
quotes (U+0034). And I'm a regex newbie. Maybe this is a question for
comp.lang.awk, but if someone knows how to contruct this regex, please post.
:-)

Thanks,

Gustaf

Nov 15 '05 #6
Gustaf Liljegren <gu**************@bredband.net> wrote:
But I just found another obstacle. The syntax in the input files allows
fields to be quoted if they contains spaces. So this

1 "2 3"\t\t4 \t\t5

should be splitted into

{"1", "2 3", "4", "5"}

This means whitespace should be removed only if it appears outside double
quotes (U+0034). And I'm a regex newbie. Maybe this is a question for
comp.lang.awk, but if someone knows how to contruct this regex, please post.
:-)


Ah - at that stage, I would personally ditch regular expressions and
write the parsing bit yourself. It shouldn't be too hard and the code
shouldn't be too slow afterwards.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
Jon,

I told you that someone else would be better suited in crafting the
regular expression! =)
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
You might want to take a look at regular expressions for this. There are escape sequences which can eliminate whitespace (I believe \w) and then will split apart the string appropriately.


\w is "word characters". \s is whitespace. (I say, after trying \w and
being surprised when it didn't work :)
However, someone else in the group might be better suited to providing the actual pattern. You will have to use the Regex class in the
System.Text.RegularExpressions namespace.


I believe the pattern wanted here is @"\s+" with the string being
trimmed to start with.

Here's a sample program:

using System;
using System.Text.RegularExpressions;

class Test
{
static void Main()
{
string t = " 1 2\t\t3 \t\t4 ";

string[] x = Regex.Split (t.Trim(), @"\s+");
foreach (string s in x)
{
Console.WriteLine ("'{0}'", s);
}
}
}

That produces the output:

'1'
'2'
'3'
'4'

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #8
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
Gustaf Liljegren <gu**************@bredband.net> wrote:
This means whitespace should be removed only if it appears outside
double quotes (U+0034). And I'm a regex newbie. Maybe this is a
question for comp.lang.awk, but if someone knows how to contruct this
regex, please post.
:-)


Ah - at that stage, I would personally ditch regular expressions and
write the parsing bit yourself. It shouldn't be too hard and the code
shouldn't be too slow afterwards.


I wrote some code that needed to do this as well - my solution was still to
split with the Regex, but then I do some checking to look for double quotes
at the start and end of the strings, and when I find a pair I just join
them back together. Of course this means you will lose the knowledge of
exactly WHAT whitespace ("was it a tab or a space?") was separating them,
but in my case it didn't matter... just a thought.

-mbray
Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Evan Escently | last post: by
10 posts views Thread by Ian Todd | last post: by
8 posts views Thread by Richard Hollenbeck | last post: by
21 posts views Thread by c | last post: by
19 posts views Thread by pkirk25 | last post: by
17 posts views Thread by kleary00 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.