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

Delimited String to Array

P: n/a
New to C# (migrating from Delphi) and I'm not sure how to get a
delimited string into an array of string.

input: string looks like - 01-85-78-15-Q11
output: an array with elements - 01,85,78,15 etc...

Is there some sort of easy one liner that does this? Hard to search
the help files for anything that would assist me in doing this.

Thanks in advance.

Aug 11 '06 #1
Share this Question
Share on Google+
18 Replies


P: n/a
"Kyro" <ky*****@gmail.comwrote in news:1155322984.111975.115810
@i3g2000cwc.googlegroups.com:
New to C# (migrating from Delphi) and I'm not sure how to get a
delimited string into an array of string.

input: string looks like - 01-85-78-15-Q11
output: an array with elements - 01,85,78,15 etc...

Is there some sort of easy one liner that does this? Hard to search
the help files for anything that would assist me in doing this.
You have two options:

1) Easy - use string.Split.

string input = "01-85-78";
string[] output = input.Split('-');

2) Easy, more flexible, but perhaps overkill - use Regex

string input = "01-85-78";
string[] output = Regex.Split(input, "-");

I would only use the Regex option if you get into more complicated
splitting requirements.

-mdb

Aug 11 '06 #2

P: n/a
Hello Kyro,

See String.Split/String.Join methods

KNew to C# (migrating from Delphi) and I'm not sure how to get a
Kdelimited string into an array of string.
K>
Kinput: string looks like - 01-85-78-15-Q11
Koutput: an array with elements - 01,85,78,15 etc...
KIs there some sort of easy one liner that does this? Hard to search
Kthe help files for anything that would assist me in doing this.
K>
KThanks in advance.
K>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Aug 11 '06 #3

P: n/a
Hello Kyro,

BTW, u even could use String.Replace

KNew to C# (migrating from Delphi) and I'm not sure how to get a
Kdelimited string into an array of string.
K>
Kinput: string looks like - 01-85-78-15-Q11
Koutput: an array with elements - 01,85,78,15 etc...
KIs there some sort of easy one liner that does this? Hard to search
Kthe help files for anything that would assist me in doing this.
K>
KThanks in advance.
K>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Aug 11 '06 #4

P: n/a

Thanks for all responses.
Michael Nemtsev wrote:
Hello Kyro,

BTW, u even could use String.Replace

KNew to C# (migrating from Delphi) and I'm not sure how to get a
Kdelimited string into an array of string.
K>
Kinput: string looks like - 01-85-78-15-Q11
Koutput: an array with elements - 01,85,78,15 etc...
KIs there some sort of easy one liner that does this? Hard to search
Kthe help files for anything that would assist me in doing this.
K>
KThanks in advance.
K>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Aug 11 '06 #5

P: n/a
Fun with strings

string[] mystring = ((string)"1,2,3,4,5").Split(new char[] {
char.Parse(",")} );

"Kyro" <ky*****@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
New to C# (migrating from Delphi) and I'm not sure how to get a
delimited string into an array of string.

input: string looks like - 01-85-78-15-Q11
output: an array with elements - 01,85,78,15 etc...

Is there some sort of easy one liner that does this? Hard to search
the help files for anything that would assist me in doing this.

Thanks in advance.

Aug 11 '06 #6

P: n/a
"AMDRIT" <am****@hotmail.comha scritto nel messaggio
news:Ox**************@TK2MSFTNGP02.phx.gbl...
Fun with strings

string[] mystring = ((string)"1,2,3,4,5").Split(new char[] {
char.Parse(",")} );
wow!
How to complicate a simple thing :)

what about

string[] mystring = "1,2,3,4,5".Split(',');

?

--

Free .Net Reporting Tool - http://www.neodatatype.net
Aug 11 '06 #7

P: n/a
Bah, C# is already a complicated language

The guy said that he was moving from delphi to C# and was working with
string. Perhaps he could also glean additional information from the errata
that I through in there.

Running both lines in code analysis neither way violated a condition. Does
my way cause more cpu cycles once it is compiled, or does your way actually
do the same thing once compiled? Is boxing wrong to do?

I'll go back to my side of the fence now and play in my VB sandbox.

"Fabio" <zn*******@virgilio.itwrote in message
news:Oc**************@TK2MSFTNGP05.phx.gbl...
"AMDRIT" <am****@hotmail.comha scritto nel messaggio
news:Ox**************@TK2MSFTNGP02.phx.gbl...
>Fun with strings

string[] mystring = ((string)"1,2,3,4,5").Split(new char[] {
char.Parse(",")} );

wow!
How to complicate a simple thing :)

what about

string[] mystring = "1,2,3,4,5".Split(',');

?

--

Free .Net Reporting Tool - http://www.neodatatype.net

Aug 11 '06 #8

P: n/a
AMDRIT <am****@hotmail.comwrote:
Bah, C# is already a complicated language

The guy said that he was moving from delphi to C# and was working with
string. Perhaps he could also glean additional information from the errata
that I through in there.

Running both lines in code analysis neither way violated a condition. Does
my way cause more cpu cycles once it is compiled, or does your way actually
do the same thing once compiled? Is boxing wrong to do?

I'll go back to my side of the fence now and play in my VB sandbox.
Well, both ways create a new array each time. However, yours calls
char.Parse unnecessarily.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 11 '06 #9

P: n/a
Michael Nemtsev <ne*****@msn.comwrote:
BTW, u even could use String.Replace
How would string.Replace convert it into an array?
KNew to C# (migrating from Delphi) and I'm not sure how to get a
Kdelimited string into an array of string.
K>
Kinput: string looks like - 01-85-78-15-Q11
Koutput: an array with elements - 01,85,78,15 etc...
KIs there some sort of easy one liner that does this? Hard to search
Kthe help files for anything that would assist me in doing this.
K>
KThanks in advance.
K>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 11 '06 #10

P: n/a
Hello Jon Skeet [C# MVP],

Not in array, just replace "-" on "," without converting into array
I just missed that OP need output array not the string with "," delimiter :)
>BTW, u even could use String.Replace
JHow would string.Replace convert it into an array?
J>
>KNew to C# (migrating from Delphi) and I'm not sure how to get a
Kdelimited string into an array of string.
K>
Kinput: string looks like - 01-85-78-15-Q11
Koutput: an array with elements - 01,85,78,15 etc...
KIs there some sort of easy one liner that does this? Hard to
search
Kthe help files for anything that would assist me in doing this.
K>
KThanks in advance.
K>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour
"At times one remains faithful to a cause only because its opponents
do not cease to be insipid." (c) Friedrich Nietzsche
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Aug 11 '06 #11

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.comha scritto nel messaggio
>
Well, both ways create a new array each time. However, yours calls
char.Parse unnecessarily.
And the cast to string on a string is unnecessarily too.
--

Free .Net Reporting Tool - http://www.neodatatype.net
Aug 12 '06 #12

P: n/a
Fabio <zn*******@virgilio.itwrote:
Well, both ways create a new array each time. However, yours calls
char.Parse unnecessarily.

And the cast to string on a string is unnecessarily too.
That's true, but I don't believe it'll have any effect on performance.
(I would expect it to be removed from the IL, as it's provably a no-op
at compile-time.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 12 '06 #13

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.comha scritto nel messaggio
>And the cast to string on a string is unnecessarily too.

That's true, but I don't believe it'll have any effect on performance.
(I would expect it to be removed from the IL, as it's provably a no-op
at compile-time.)
It can be true, but for the user is time to write it and difficult to read.
--

Free .Net Reporting Tool - http://www.neodatatype.net
Aug 12 '06 #14

P: n/a
Fabio <zn*******@virgilio.itwrote:
And the cast to string on a string is unnecessarily too.
That's true, but I don't believe it'll have any effect on performance.
(I would expect it to be removed from the IL, as it's provably a no-op
at compile-time.)

It can be true, but for the user is time to write it and difficult to read.
Oh absolutely - it's a silly thing to do, but the post I was answering
was talking about performance, not about readability.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 12 '06 #15

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.comha scritto nel messaggio
Oh absolutely - it's a silly thing to do, but the post I was answering
was talking about performance, not about readability.
The post asked for "some sort of easy one liner that does this", and I think
this is about the code, not the compiler.

:)

And in all the ways I prefere a good code than a good IL.

As someone says "every fool can write code that a compiler can read, a good
programmer write code that humans can read".
Aug 12 '06 #16

P: n/a
Fabio <zn*******@virgilio.itwrote:
Oh absolutely - it's a silly thing to do, but the post I was answering
was talking about performance, not about readability.

The post asked for "some sort of easy one liner that does this", and I think
this is about the code, not the compiler.
The original one did, yes. Not the one I was replying to.
And in all the ways I prefere a good code than a good IL.
Absolutely.
As someone says "every fool can write code that a compiler can read, a good
programmer write code that humans can read".
I don't disagree at all. I was just answering the question Amdrit
asked.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 12 '06 #17

P: n/a
When talking about human readability, that would be why I would be as
complete as possible. Obviously, if the casting was not required, leave it
out, I just didn't know better.

in the case of a single if statement, I find the shorthand cumbersome and
difficult to read. Any thoughts?

I get lost when I see
[C#}
if (foo==bar)
//do something

Where as this is highly readble to me
{C#]
if (foo==bar)
{
//do something
}

Conversly, this seem readable to me
[VB]
if foo = bar then 'Do something
My biggest hang up with c sharp is that I am an old vb developer and the
c-like syntanx can get rough to read. I attempt to write complete code when
I make use of the language.

Another hangup I have is:

string[] mystring =
"1,2,3,4,5".split(",");

Maybe it is just becuse it is missing the "_". Only in vb we wouldn't be
able to use "_" there. Not to mention the use of variables with case
differences.

private object apple = null;

object Apple()
{
get{return apple;}
set{apple = value;}
}

I do like the quick and dirty property construct, I don't like that it is
not easily described as a property, that the understood value is the
underlying setter value.

All of these things make the langauge not easily human readable. Perhaps
programmer readable, but for one that has experience with c-like languages.
Don't get me wrong, I like c-sharp well enough, I just wish that the VB
constructs and languages were closer to the c-like cousins and then tasking
between the two wouldn't be so bad.

After all, in vb to convert the string to the array:

Dim myStrings() as String = "1,2,3,4,5".Split(",")

that is how I would do it there, knowing that the compiler will not have to
worry about any conversions.

So while this is preferred

string[] myString = "1,2,3,4,5".split(",");

Where is it made clear to use the type casting and not to use it?
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Fabio <zn*******@virgilio.itwrote:
Oh absolutely - it's a silly thing to do, but the post I was answering
was talking about performance, not about readability.

The post asked for "some sort of easy one liner that does this", and I
think
this is about the code, not the compiler.

The original one did, yes. Not the one I was replying to.
>And in all the ways I prefere a good code than a good IL.

Absolutely.
>As someone says "every fool can write code that a compiler can read, a
good
programmer write code that humans can read".

I don't disagree at all. I was just answering the question Amdrit
asked.

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

Aug 14 '06 #18

P: n/a
AMDRIT <am****@hotmail.comwrote:
When talking about human readability, that would be why I would be as
complete as possible. Obviously, if the casting was not required, leave it
out, I just didn't know better.
The casting is not required, and neither is the parsing of the string
to a character.

So, to make explicit what the compiler does implicitly:

string[] mystring = "1,2,3,4,5".Split(new char[]{','});

Personally I'd still rather see

string[] mystring = "1,2,3,4,5".Split(',');

because it's easier to see what the *aim* is that way, even though it's
not as clear that an extra char array is being created.
in the case of a single if statement, I find the shorthand cumbersome and
difficult to read. Any thoughts?

I get lost when I see
[C#}
if (foo==bar)
//do something

Where as this is highly readble to me
{C#]
if (foo==bar)
{
//do something
}
I wouldn't say I get lost with the first way, but it's more error-
prone. I always include the braces.
Conversly, this seem readable to me
[VB]
if foo = bar then 'Do something
My biggest hang up with c sharp is that I am an old vb developer and the
c-like syntanx can get rough to read. I attempt to write complete code when
I make use of the language.

Another hangup I have is:

string[] mystring =
"1,2,3,4,5".split(",");

Maybe it is just becuse it is missing the "_". Only in vb we wouldn't be
able to use "_" there.
Well, it depends on the situation. I don't break lines for the sake of
it, but I do break them when they're getting too long.
Not to mention the use of variables with case differences.

private object apple = null;

object Apple()
{
get{return apple;}
set{apple = value;}
}
That's using a variable with a different case to a property, not two
variables with case differences.
I do like the quick and dirty property construct, I don't like that it is
not easily described as a property, that the understood value is the
underlying setter value.
The fact that it's capitalised should indicate that it's either a
readonly variable or a property.
All of these things make the langauge not easily human readable. Perhaps
programmer readable, but for one that has experience with c-like languages.
In the above case, it's more that one has experience with the .NET
conventions.
Don't get me wrong, I like c-sharp well enough, I just wish that the VB
constructs and languages were closer to the c-like cousins and then tasking
between the two wouldn't be so bad.

After all, in vb to convert the string to the array:

Dim myStrings() as String = "1,2,3,4,5".Split(",")

that is how I would do it there, knowing that the compiler will not have to
worry about any conversions.
No, the compiler *is* having to do conversions - indeed, the above
won't even compile when you've got option strict on. That's creating a
new char array, then loading it with the first character of a string.
So while this is preferred

string[] myString = "1,2,3,4,5".split(",");
No, that doesn't work. It has to be a *char*, but the char is
automatically converted to an array because the parameter is declared
with "params".
Where is it made clear to use the type casting and not to use it?
Well, "1,2,3,4,5" is always a string - it's a string literal - so
there's no need to cast, and doing so really doesn't help the reader at
all.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 14 '06 #19

This discussion thread is closed

Replies have been disabled for this discussion.