473,503 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delimited String to Array

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
18 3602
"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
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
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

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
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
"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
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
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
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
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
"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
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
"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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2807
by: Arne | last post by:
From: "Arne de Booij" <a_de_booij@hotmail.com> Subject: Comma delimited array into DB problems Date: 9. februar 2004 10:39 Hi, I have an asp page that takes input from a form on the previous...
5
1720
by: CJM | last post by:
In an application I'm working the user has the opportunity to record the despatching of one or more items with serial numbers. For each item they despatch, they have to chose the serial no that...
4
6838
by: Vagabond Software | last post by:
Apparently, the Split method handles consecutive tabs as a single delimiter. Does anyone have any suggestions for handling consecutive tabs? I am reading in text files that contain lines of...
2
1463
by: Ot | last post by:
I have a Tab delimited file: Field1<tab>Field2<tab>...<Field20><crlf> There must be an easy way to break this apart, but I haven't found it yet. Can anyone help? Regards, Ot
8
1520
by: Mark | last post by:
Supposing I have a string containing fields delimited by | (pipe) such as: this|is|a|string|delimited|by|pipes If I wanted to get the portion of the string between the 3rd and 4th pipe, I...
3
9642
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
2
2519
by: gh | last post by:
Hi, I have a string variable which contains n number of comma delimited elements and I would like to store each element into an array but I could not figure how to do it. for example,...
15
9530
by: VMI | last post by:
I'm parsing a comma-delimited record but I want it to do something if some of the string is between "". How can I do this? With the Excel import it does it correct. I'm using String.Split()....
2
4694
by: Jim | last post by:
Hello: I have a variable which contains a comma delimited list contained in a database variable e.g. "5,6,9,10,55,42" - I want to hand this list to an array function to get it to populate an...
0
7192
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
7261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.