reading values from strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SoftwareTester
    New Member
    • Jan 2008
    • 13

    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?
  • babai28
    New Member
    • Jul 2008
    • 59

    #2
    try this code:

    Code:
    try
                {
                    ushort num;
                    int counter=-1;
                    string line = "1 12 15 12 8 21";
                    string[] str = line.Split(' ');
                    ushort[] Numbers = new ushort[str.Length];
                    foreach(string piece in str)
                    {
                        counter++;
                        if (UInt16.TryParse(piece, out num))
                        {
                            Numbers[counter]=num;
                        }
                    }
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    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:

    Code:
    if(Numbers[i].ToString()==String.Empty)
    All the best,

    Comment

    • vekipeki
      Recognized Expert New Member
      • Nov 2007
      • 229

      #3
      You should start with String.Split, but try to do the rest yourself.

      Comment

      • SoftwareTester
        New Member
        • Jan 2008
        • 13

        #4
        Somewhere I read about regular expressions and (based upon what I read) I managed to get this working:
        Code:
            using System.Text.RegularExpressions;
        
                                    Regex Spaces = new Regex(@"\s+");
                                    string[] Fields = Spaces.Split(Line.Trim());
                                    if (Fields != null)
                                    {
                                        int Count = Fields.Length;
                                        ushort[] Numbers = new ushort[Count];
                                        for (int i=0; i<Count; i++)
                                       {
                                           Numbers[i] = ushort.Parse(Fields[i]);
                                       }
                                   }
        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
        Code:
        DIMENSION NUMBERS(6)
        Read(Line,'(BN,I2)', Numbers)
        being just ONE line (part of the language!!) for reading numbers from a string instead of a whole function to write.......... .

        Comment

        • vekipeki
          Recognized Expert New Member
          • Nov 2007
          • 229

          #5
          I guess you can make one long line out of it, to simplify it:

          Code:
          int[] Numbers = new List<int>(
              new List<String>(Line.Split(' ','\t'))
              .FindAll(delegate(string token)
                { return !string.IsNullOrEmpty(token); })
              .ConvertAll<int>(delegate(string token)
                { return int.Parse(token); })
              ).ToArray();

          "To simplify it" - yeah right!

          Just kidding :)

          Comment

          • kunal pawar
            Contributor
            • Oct 2007
            • 297

            #6
            I think "String.Spl it" is best solution as suggested by vekipeki

            Comment

            • SoftwareTester
              New Member
              • Jan 2008
              • 13

              #7
              Thanks,

              I'll test which one is fastest and put that one into a function

              Comment

              Working...