By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,411 Members | 1,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,411 IT Pros & Developers. It's quick & easy.

Convert to an Array

P: n/a
How would I convert this string to an Array?
"243, 567, 324, 345"
Where each number becomes a new element in that Array.
Thanks
Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I'm not sure if you want a string array or an integer array but this should
help:

using System.Text.RegularExpressions;
....

Regex rx = new Regex(", ");
string[] myarray = rx.Split("243, 567, 324, 345"); // creates a string array
containing each number

foreach (string s in myarray)
{
// show the string version of this array item
MessageBox.Show("'" + s + "'");

// Convert it to an integer if you like
int myinteger = Int32.Parse(s);
// add the integer to an array here if you like...
}
ShaneB

"Tome73" <To****@discussions.microsoft.com> wrote in message
news:2D**********************************@microsof t.com...
How would I convert this string to an Array?
"243, 567, 324, 345"
Where each number becomes a new element in that Array.
Thanks

Nov 22 '05 #2

P: n/a
For a simple split like this one, you can easily use the Split function
of the String class. Check your online help for examples.

hth, Philipp

Tome73 wrote:
How would I convert this string to an Array?
"243, 567, 324, 345"
Where each number becomes a new element in that Array.
Thanks

Nov 22 '05 #3

P: n/a
The reason I didn't mention string.split is because it won't work as
expected in his case. In the original poster's question, the string he
provided contained a space after each comma. That means the code would have
to look like this:

string s = "243, 567, 324, 345";
string[] myarray = s.Split(new char[]{',' , " "});

As a result, myarray will contains 8 items...because string.split split the
array on each occurance of each character.

Regex.Split() will split it properly and accepts a string input as well.

ShaneB
"Philipp Sumi" <sp*******@thisdomaindoesntexist.org> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
For a simple split like this one, you can easily use the Split function of
the String class. Check your online help for examples.

hth, Philipp

Tome73 wrote:
How would I convert this string to an Array?
"243, 567, 324, 345"
Where each number becomes a new element in that Array. Thanks

Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.