473,385 Members | 1,888 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,385 software developers and data experts.

reading values from strings

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
6 1608
babai28
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
229 Expert 100+
You should start with String.Split, but try to do the rest yourself.
Feb 27 '09 #3
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
229 Expert 100+
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
297 100+
I think "String.Split" is best solution as suggested by vekipeki
Mar 2 '09 #6
Thanks,

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

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

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
24
by: Hendrik Schober | last post by:
Hi, I have a 'std::istream' and need to read its whole contents into a string. How can I do this? TIA; Schobi
8
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a...
1
by: Sunil Pathi | last post by:
Thanks in Advance I have a XML file which is read from a .aspx page. I need to read through all the elements, find the values of them and pass them on to the stored procedure which updates the...
21
by: EdUarDo | last post by:
Hi all, I'm not a newbie with C, but I don't use it since more than 5 years... I'm trying to read a text file which has doubles in it: 1.0 1.1 1.2 1.3 1.4 2.0 2.1 2.2 2.3 2.4 I'm doing...
7
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that...
18
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
6
by: jcasique.torres | last post by:
Hi everyboy. I trying to create a C promang in an AIX System to read JPG files but when it read just the first 4 bytes when it found a DLE character (^P) doesn't read anymore. I using fread...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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.