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

reading values from strings

P: 13
In memory I have a string containing numbers (ushorts) I want to read into an array.

Example :
string Line = " 1 12 15 12 8 21 ";
ushort[] Numbers = new ushort[6];

I want :
Numbers[0] = 1
Numbers[1] =12
Numbers[2] =15
Numbers[3] = 12
Numbers[4] = 8
Numbers[5] =21

How can I do that?
Feb 27 '09 #1
Share this Question
Share on Google+
6 Replies


P: 59
try this code:

Expand|Select|Wrap|Line Numbers
  1. try
  2.             {
  3.                 ushort num;
  4.                 int counter=-1;
  5.                 string line = "1 12 15 12 8 21";
  6.                 string[] str = line.Split(' ');
  7.                 ushort[] Numbers = new ushort[str.Length];
  8.                 foreach(string piece in str)
  9.                 {
  10.                     counter++;
  11.                     if (UInt16.TryParse(piece, out num))
  12.                     {
  13.                         Numbers[counter]=num;
  14.                     }
  15.                 }
  16.  
  17.             }
  18.             catch (Exception ex)
  19.             {
  20.                 MessageBox.Show(ex.Message);
  21.             }
However mind the following words of caution:
While accessing the array Numbers you have to first check if the array element contain a value or not.
For example, if you supple the string line as " 2 3 f 4 5"
The Number array will have values like
Numbers[0] = 2
Numbers[1] =3
Numbers[2] =null
Numbers[3] = 4
Numbers[4] = 5

By slightly changing the code, however, you can avoid this problem. Also you can provide a check like:

Expand|Select|Wrap|Line Numbers
  1. if(Numbers[i].ToString()==String.Empty)
All the best,
Feb 27 '09 #2

vekipeki
Expert 100+
P: 229
You should start with String.Split, but try to do the rest yourself.
Feb 27 '09 #3

P: 13
Somewhere I read about regular expressions and (based upon what I read) I managed to get this working:
Expand|Select|Wrap|Line Numbers
  1.     using System.Text.RegularExpressions;
  2.  
  3.                             Regex Spaces = new Regex(@"\s+");
  4.                             string[] Fields = Spaces.Split(Line.Trim());
  5.                             if (Fields != null)
  6.                             {
  7.                                 int Count = Fields.Length;
  8.                                 ushort[] Numbers = new ushort[Count];
  9.                                 for (int i=0; i<Count; i++)
  10.                                {
  11.                                    Numbers[i] = ushort.Parse(Fields[i]);
  12.                                }
  13.                            }
  14.  
  15.  
it takes care of tabs as well.

Although it has been a long time ago I have been using FORTRAN-77 (and because of that not totally sure about the EXACT syntax anymore) making it possible to read
Expand|Select|Wrap|Line Numbers
  1. DIMENSION NUMBERS(6)
  2. Read(Line,'(BN,I2)', Numbers)
  3.  
being just ONE line (part of the language!!) for reading numbers from a string instead of a whole function to write...........
Feb 28 '09 #4

vekipeki
Expert 100+
P: 229
I guess you can make one long line out of it, to simplify it:

Expand|Select|Wrap|Line Numbers
  1. int[] Numbers = new List<int>(
  2.     new List<String>(Line.Split(' ','\t'))
  3.     .FindAll(delegate(string token)
  4.       { return !string.IsNullOrEmpty(token); })
  5.     .ConvertAll<int>(delegate(string token)
  6.       { return int.Parse(token); })
  7.     ).ToArray();

"To simplify it" - yeah right!

Just kidding :)
Mar 2 '09 #5

kunal pawar
100+
P: 297
I think "String.Split" is best solution as suggested by vekipeki
Mar 2 '09 #6

P: 13
Thanks,

I'll test which one is fastest and put that one into a function
Mar 2 '09 #7

Post your reply

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