473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2368
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Evan Escently | last post by:
Hi, I've laid out a _very_ simple database that tracks my artwork the table 'works' looks like: +---------+----------+------------+------------+-------------+ | work_id | title | media ...
10
by: Ian Todd | last post by:
Hi, I am trying to read in a list of data from a file. Each line has a string in its first column. This is what i want to read. I could start by saying char to read in 1000 lines to the array( i...
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
8
by: Richard Hollenbeck | last post by:
I have a recipe database that I've been building but I haven't yet put any of the ingredients in because of a little problem of normalization. If I build a table of ingredients, all the recipes...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
21
by: c | last post by:
Hi everybody. I'm working on converting a program wriiten on perl to C, and facing a problem with concatenate strings. Now here is a small program that descripe the problem, if you help me to...
19
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html contains the following data: Bladefist-Horde...
17
by: kleary00 | last post by:
Hi, I am writing a function that needs to return an array of strings and I am having some trouble getting it right. I need some help. Here is what I consider an array of 100 strings: char...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.