473,387 Members | 3,684 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,387 software developers and data experts.

String Array Parsed to two new String Arrays

I am really confused on this one. I have 5 elements in a string array.

Expand|Select|Wrap|Line Numbers
  1. string[] strings = { "Widget 15.50", "Thingy 50.99", "Ratchet25.00", "Clanger115.49", "Fracker75.25" };
I have to separate the text from the numbers into two new arrays. One string containing {"Widget ", "Thingy", "Ratchet", "Clanger", "Fracker"} and the second a double array containing {15.50, 50.99, 25.00, 115.49, 75.25}

I don't know how to do this with an array. Can someone help?
Nov 6 '11 #1

✓ answered by Paul Johnson

Something like this might work

Expand|Select|Wrap|Line Numbers
  1. string[] strings = { "Widget 15.50", "Thingy 50.99", "Ratchet25.00", "Clanger115.49", "Fracker75.25" };
  2. double[] values = new double[4];
  3. int n = 0;
  4. foreach(string s in strings)
  5. {
  6. string[] foo = s.split(' ');
  7. values[n++] = Convert.ToDouble(foo[1])
  8. }
  9.  
Though a far better way would be to have this

Expand|Select|Wrap|Line Numbers
  1. List<string> things = new List<string>();
  2. List<double> values = new List<double>();
  3. string[] strings = { "Widget 15.50", "Thingy 50.99", "Ratchet25.00", "Clanger115.49", "Fracker75.25" };
  4. foreach(string s in strings)
  5. {
  6.   string[] foo = s.split(' ');
  7.   things.Add(foo[0]);
  8.   values.Add(Convert.ToDouble(foo[1]);
  9. }
  10.  
Far less hassle than using arrays....

2 2189
Something like this might work

Expand|Select|Wrap|Line Numbers
  1. string[] strings = { "Widget 15.50", "Thingy 50.99", "Ratchet25.00", "Clanger115.49", "Fracker75.25" };
  2. double[] values = new double[4];
  3. int n = 0;
  4. foreach(string s in strings)
  5. {
  6. string[] foo = s.split(' ');
  7. values[n++] = Convert.ToDouble(foo[1])
  8. }
  9.  
Though a far better way would be to have this

Expand|Select|Wrap|Line Numbers
  1. List<string> things = new List<string>();
  2. List<double> values = new List<double>();
  3. string[] strings = { "Widget 15.50", "Thingy 50.99", "Ratchet25.00", "Clanger115.49", "Fracker75.25" };
  4. foreach(string s in strings)
  5. {
  6.   string[] foo = s.split(' ');
  7.   things.Add(foo[0]);
  8.   values.Add(Convert.ToDouble(foo[1]);
  9. }
  10.  
Far less hassle than using arrays....
Nov 7 '11 #2
Awesome!! Thank you so much!!!
Nov 8 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Keiron Waites | last post by:
I get the following error: Notice: Array to string conversion in C:\Documents and Settings\ShepMode\Desktop\Websites\ShareMonkey.net\Web\join.php on line 11 in this code: $input =...
1
by: john | last post by:
Relatively new to C coding, so any help would greatly be appreciated. I'm having problems try to return my string array from my parsing function. When I do a printf I am getting the correct value...
4
by: Garfield | last post by:
Hello I have a function that returns a string array. The string has a name and an ID number, they are separated by a comma. I cannot for the life of me by using the string methods get to...
0
by: NuBBeR | last post by:
I have a string that i have split into an array and what i would like to do is go through each of the items in the array and parse 3 substrings for that string: string str = (temp.substr(start,...
2
by: zlf | last post by:
Hi, This there any existing alorgithm in .NET that can extract the common element in two string arrays to another string array; usage: string a = { "1","2","3" }; string b = { "1" }; string...
12
by: DumberThanSnot | last post by:
is there a faster way to copy an ArrayList of strings to a string other than a tight loop. In it's most simple terms, I'm currently using something like this... ---------------------------- ...
5
by: Paulers | last post by:
Hello all, I have a string array with duplicate elements. I need to create a new string array containing only the unique elements. Is there an easy way to do this? I have tried looping through...
1
by: dotnetnoob | last post by:
i have a arraylist that store String() array the string array hold value 1 2 4 i want to access the string array that is inside the arraylist one arraylist item at the time i search up and...
2
by: LinLMa | last post by:
Hello everyone, I find strange result in the following program. 1. For string array, dereferencing it will result in the string itself, but for int array, dereferencing it will result in the...
8
by: David Lazos | last post by:
Hi All, I use Contains method of String object to determine if a string variable has another string, like that. *************************** ipAddress.Contains("127.0.0")...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.