473,604 Members | 2,487 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 3622
"Kyro" <ky*****@gmail. comwrote in news:1155322984 .111975.115810
@i3g2000cwc.goo glegroups.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(inp ut, "-");

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(ne w char[] {
char.Parse(",") } );

"Kyro" <ky*****@gmail. comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.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******** ******@TK2MSFTN GP02.phx.gbl...
Fun with strings

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

what about

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

?

--

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*******@virg ilio.itwrote in message
news:Oc******** ******@TK2MSFTN GP05.phx.gbl...
"AMDRIT" <am****@hotmail .comha scritto nel messaggio
news:Ox******** ******@TK2MSFTN GP02.phx.gbl...
>Fun with strings

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

wow!
How to complicate a simple thing :)

what about

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

?

--

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.co m>
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.co mwrote:
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.co m>
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2813
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 page, puts that into an array and inserts the array into SQL server. Now here is the problem:
5
1726
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 they want to despatch from a list of available ones. In many cases, these items will be of the same time, so the dropdown of available serial no's may be the same. So now I need to check that the user hasnt picked the same serial no for two...
4
6857
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 tab-delimited data. I was using string stringArray = lineOfText.Split('\t') to automatically populate an array used to populate the values in a new DataRow. However, sometimes the lines of text contain null values. I can find these null values by...
2
1484
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
1525
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 would have to start at the beginning of the entire string and inspect each character to see if it is a pipe. Once I found the 3rd pipe, I could begin parsing the string until a 4th pipe is discovered. Is there a simpler and more elegant way to do...
3
9653
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... field1...field2...field3...
2
2533
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, 1stitem,2nditem,3rditem,4thitem, ..., nthitem What would be the best way of storing the above string into an array like
15
9544
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(). Basically, this is what I want to do: Use string.Split() on the whole string UNLESS the string is in between double-quotes. The part of the string in-between the "" will be ignored by String.Split Thanks.
2
4703
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 array, such as follows (where rst!Q10ACode is the database variable): Dim ListArray as Variant Dim Substring1 as String
0
7929
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8419
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8065
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8280
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5441
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3955
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2434
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1526
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.