469,913 Members | 2,683 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,913 developers. It's quick & easy.

Populate Array

Hi All,

I have a string which has product names, which are seperated by comma. The
number of products in the string are random.
I want to populate the array using the string, one product per level.

Please let me know how to do it.

Thanks in Advance,
Anil
Nov 16 '05 #1
2 6305

Have a look at the documentation for String.Split


"Anil" <an******@yahoo.com> wrote in message
news:uO**************@TK2MSFTNGP10.phx.gbl...
Hi All,

I have a string which has product names, which are seperated by comma. The
number of products in the string are random.
I want to populate the array using the string, one product per level.

Please let me know how to do it.

Thanks in Advance,
Anil

Nov 16 '05 #2
Anil,

Try this out.

// generate a random comma-delimited string for your product names
const string randChars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ";
Random r = new Random();

const string delimiters = ","; // characters that count as delimiters
int maxNames = 10; // maximum number of random names to generate
int maxLength = 32; // maximum length of characters in random name string

System.Text.StringBuilder sb = new System.Text.StringBuilder();

// O(maxNames * maxLength) loop to generate names
for (int i = 0; i < r.Next(maxNames+1); i++)
{
// generate a name by picking one character at a time
for (int j = 0; j < r.Next(maxLength+1); j++)
{
// append a random alphabetic character to the end of the current name
sb.Append(randChars.Substring(r.Next(randChars.Len gth), 1));
}
// append a random delimiter character to the end and start a new name
sb.Append(delimiters.Substring(r.Next(delimiters.L ength), 1));
}

// get the product names into a string array
string[] prodNames = sb.ToString().Split(delimiters.ToCharArray());

// print the product names
foreach (string s in prodNames)
{
Console.WriteLine("Product name found: {0}", s);
}
I have a string which has product names, which are seperated by comma. The
number of products in the string are random.
You can see that the number of products is indeterminate at compile-time
(it's random!), so this solution should work in general if you want to test
the Split method out on random product names. The Split method is what you
want to use here to get the individual strings.

Best wishes,
JJ Feminella

"Anil" <an******@yahoo.com> wrote in message
news:uO**************@TK2MSFTNGP10.phx.gbl... Hi All,

I have a string which has product names, which are seperated by comma. The
number of products in the string are random.
I want to populate the array using the string, one product per level.

Please let me know how to do it.

Thanks in Advance,
Anil

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Greg Scharlemann | last post: by
4 posts views Thread by Kevin H | last post: by
2 posts views Thread by George | last post: by
11 posts views Thread by Zordiac | last post: by
6 posts views Thread by Beginner | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.