Delimited String to Array

  • Thread starter Thread starter Kyro
  • Start date Start date
K

Kyro

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.
 
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
 
Hello Kyro,

See String.Split/String.Join methods

K> New to C# (migrating from Delphi) and I'm not sure how to get a
K> delimited string into an array of string.
K>
K> input: string looks like - 01-85-78-15-Q11
K> output: an array with elements - 01,85,78,15 etc...
K> Is there some sort of easy one liner that does this? Hard to search
K> the help files for anything that would assist me in doing this.
K>
K> Thanks 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
 
Hello Kyro,

BTW, u even could use String.Replace

K> New to C# (migrating from Delphi) and I'm not sure how to get a
K> delimited string into an array of string.
K>
K> input: string looks like - 01-85-78-15-Q11
K> output: an array with elements - 01,85,78,15 etc...
K> Is there some sort of easy one liner that does this? Hard to search
K> the help files for anything that would assist me in doing this.
K>
K> Thanks 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 Nietzsch
 
Fun with strings

string[] mystring = ((string)"1,2,3,4,5").Split(new char[] {
char.Parse(",")} );
 
AMDRIT said:
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(',');

?
 
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 said:
AMDRIT said:
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(',');

?
 
AMDRIT said:
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.
 
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 :)
J> How would string.Replace convert it into an array?
J>---
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 said:
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.
 
Fabio said:
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 said:
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.
 
Fabio said:
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 said:
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".
 
Fabio said:
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.
 
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?
 
AMDRIT said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top