splitting a string out to an array of strings

B

BobRoyAce

Let's say I have a string sItemString = "item1~item2~item3" and a string
array asItemArray[]. In the VB world, I could use the Split function as
follows: asItemArray = Split(sItemString, "~") to split out the individual
items in the string into separate elements of the array. How could I do this
in C#?
 
D

DBC User

if you have

string sItemString = "item1~item2~item3"

try this

string[] columns = record.Split('~');

this will result in
columns[0] = item1
columns[1] = item2
columns[2] = item3
 
S

Samuel R. Neff

Use the Split method.

itemArray = itemString.Split('~')

HTH,

Sam


Let's say I have a string sItemString = "item1~item2~item3" and a string
array asItemArray[]. In the VB world, I could use the Split function as
follows: asItemArray = Split(sItemString, "~") to split out the individual
items in the string into separate elements of the array. How could I do this
in C#?

B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
 
D

DBC User

Sorry had the variable wrong. Try the following

if you have


string sItemString = "item1~item2~item3"


try this


string[] columns = sItemString.Split('~');


this will result in
columns[0] = item1
columns[1] = item2
columns[2] = item3
 
O

Otis Mukinfus

Sorry had the variable wrong. Try the following

if you have


string sItemString = "item1~item2~item3"


try this


string[] columns = sItemString.Split('~');

I believe the correct syntax here would be :
string[] columns = sItemString.Split(new char[] {'~'});

The Split method expects an array of char.
this will result in
columns[0] = item1
columns[1] = item2
columns[2] = item3

Otis Mukinfus
http://www.otismukinfus.com
 
J

Jon Skeet [C# MVP]

Otis Mukinfus said:
string[] columns = sItemString.Split('~');

I believe the correct syntax here would be :
string[] columns = sItemString.Split(new char[] {'~'});

The Split method expects an array of char.

But the parameter has the "params" modifier, so the two lines of code
above are equivalent.
 
D

DBC User

It works even with '~'. I have a working code which does the following

string[] columns = record.Split('~')
 

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

Top