Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Delimited String to Array

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kyro - 11 Aug 2006 20:03 GMT
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.
Michael Bray - 11 Aug 2006 20:10 GMT
"Kyro" <kyro.sk@gmail.com> wrote 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.
[quoted text clipped - 4 lines]
> 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
Michael Nemtsev - 11 Aug 2006 20:12 GMT
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
Michael Nemtsev - 11 Aug 2006 20:13 GMT
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
Kyro - 11 Aug 2006 20:19 GMT
Thanks for all responses.

> Hello Kyro,
>
[quoted text clipped - 16 lines]
> "At times one remains faithful to a cause only because its opponents do not
> cease to be insipid." (c) Friedrich Nietzsche
Jon Skeet [C# MVP] - 11 Aug 2006 22:00 GMT
> BTW, u even could use String.Replace

How would string.Replace convert it into an array?

> K> New to C# (migrating from Delphi) and I'm not sure how to get a
> K> delimited string into an array of string.
[quoted text clipped - 12 lines]
> "At times one remains faithful to a cause only because its opponents do not
> cease to be insipid." (c) Friedrich Nietzsche

Signature

Jon Skeet - <skeet@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

Michael Nemtsev - 11 Aug 2006 23:07 GMT
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

J> How would string.Replace convert it into an array?
J>
>> K> New to C# (migrating from Delphi) and I'm not sure how to get a
>> K> delimited string into an array of string.
[quoted text clipped - 12 lines]
>> "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
AMDRIT - 11 Aug 2006 20:17 GMT
Fun with strings

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

> New to C# (migrating from Delphi) and I'm not sure how to get a
> delimited string into an array of string.
[quoted text clipped - 6 lines]
>
> Thanks in advance.
Fabio - 11 Aug 2006 20:37 GMT
> 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(',');

?

Signature

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

AMDRIT - 11 Aug 2006 21:53 GMT
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.

>> Fun with strings
>>
[quoted text clipped - 9 lines]
>
> ?
Jon Skeet [C# MVP] - 11 Aug 2006 22:01 GMT
> Bah, C# is already a complicated language
>
[quoted text clipped - 7 lines]
>
> 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.

Signature

Jon Skeet - <skeet@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

Fabio - 12 Aug 2006 09:21 GMT
"Jon Skeet [C# MVP]" <skeet@pobox.com> ha 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.

Signature

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

Jon Skeet [C# MVP] - 12 Aug 2006 13:11 GMT
> > 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.)

Signature

Jon Skeet - <skeet@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

Fabio - 12 Aug 2006 14:04 GMT
"Jon Skeet [C# MVP]" <skeet@pobox.com> ha 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.

Signature

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

Jon Skeet [C# MVP] - 12 Aug 2006 15:46 GMT
> >> And the cast to string on a string is unnecessarily too.
> >
[quoted text clipped - 3 lines]
>
> 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.

Signature

Jon Skeet - <skeet@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

Fabio - 12 Aug 2006 16:35 GMT
"Jon Skeet [C# MVP]" <skeet@pobox.com> ha 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".
Jon Skeet [C# MVP] - 12 Aug 2006 19:42 GMT
> > 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.

Signature

Jon Skeet - <skeet@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

AMDRIT - 14 Aug 2006 14:48 GMT
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?

>> > Oh absolutely - it's a silly thing to do, but the post I was answering
>> > was talking about performance, not about readability.
[quoted text clipped - 15 lines]
> I don't disagree at all. I was just answering the question Amdrit
> asked.
Jon Skeet [C# MVP] - 14 Aug 2006 17:45 GMT
> 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?
[quoted text clipped - 10 lines]
>   //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]
[quoted text clipped - 11 lines]
> 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.
>
[quoted text clipped - 5 lines]
>   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
[quoted text clipped - 6 lines]
> 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.

Signature

Jon Skeet - <skeet@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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.