splitting a string out to an array of strings

  • Thread starter Thread starter BobRoyAce
  • Start date Start date
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#?
 
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
 
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.
 
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
 
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
 
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.
 
It works even with '~'. I have a working code which does the following

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