473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 6540

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Greg Scharlemann | last post by:
I would like to automatically populate a drop down menu when the page loads based on the selection of an item in a different drop down menu. I made a test page that when drop down #1 changes, drop...
10
by: Craig Keightley | last post by:
I have the following array: function showCPUs(){ //name brandID dualCore dualProcessor var cpuItem=; var cpuItem=; var cpuItem=; var cpuItem=; var cpuItem=; var cpuItem=; var cpuItem=;
4
by: Kevin H | last post by:
Apologies in advance if this sounds slow-witted, but I didn't find it here. Need to populate some textboxes on a form. While I could hard code it (the number of options aren't that high), it...
2
by: George | last post by:
Is there a fast way to transfer an Excel range to an array? Example: Excel range is E2:E300 Dim person() as string Thanks, George
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
6
by: Beginner | last post by:
Hi, I'm trying to populate a TreeView from an existing and filled in ListView (lvwBuild). lvwBuild is a basic two column listview which can have any number of rows. I would like the first...
1
by: vj | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file? I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to...
0
by: vijendra | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file?I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to...
3
by: OliveOyl3471 | last post by:
In Visual Basic.NET 2003, how do you populate a two dimensional array from a text file? I know how to check file existence, open the file for read, etc. I just can't get it to read more than one...
1
by: DavidPr | last post by:
I have a website that requires a visitor to register in order to make a post. I have one or two users that are violating the terms and every day I'm having to delete them. They just re-register,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.