none
Splitting up a string.....

    Question

  • Hi,

    Have had look at the related topics with no joy, Umm odd question I am reading back #RE=0,124,132,192 from a device under test I need to be able to get at the numbers as numbers so I have used a foreach loop to split up the string but I get a load of blanks when I send it to a label so I tried another foreach (shown below) to get rid of the spaces:
    private void button1_Click(object sender, EventArgs e)
    
            {
    
                string Reply;
    
    
    
                Reply = label1.Text;
    
                MessageBox.Show(Reply);
    
                textBox1.Text = Reply;
    
                foreach (string subString in Reply.Split(new char[] { ' ', '\n' }))
    
                {
    
                    //textBox1.Text =textBox1.Text+ " " + Reply;
    
                }
    
    
    
            }

    This appears to do nothing! I still have the spaces, I get back
     


    0
    120
    106
    110  ,  what I want is too just pull the four numbers out without any fuss.  I quake in fear of using the Regex command as that in the past has led to days of debugging!

    Glenn  

    Monday, October 19, 2009 11:02 AM

Answers

  • I hope this is the code you are looking for.

                string Reply;
                Reply = "   12, 34 ,              56";
                string[] sArr = Reply.Split(new string[] { " ", "," }, StringSplitOptions.RemoveEmptyEntries);
                int[] iArr = new int[sArr.Length];
                int i = 0;
                foreach (string subString in sArr)
                {
                    iArr[i] = Convert.ToInt32(subString);
                    i++;
                }
    • Marked as answer by GlennPatton Monday, October 19, 2009 12:27 PM
    Monday, October 19, 2009 12:01 PM

All replies

  • You can use StringSplitOptions for this issue.

    Here is a sample code.

                string org = "a,b,  c,d , e, f";
                foreach (string s in org.Split(new string[] { " ", "," }, StringSplitOptions.RemoveEmptyEntries))
                {
                    Console.WriteLine(s);
                }
    Monday, October 19, 2009 11:13 AM
  • Hi,

    Thanks for the quick reply but the splitting isn't quite right I have copied your example code over to my demo app as 
     private void button1_Click(object sender, EventArgs e)
            {
                string Reply;
    
                //Reply = label1.Text;
                Reply = "   12, 34 ,              56";
                MessageBox.Show(Reply);
                //textBox1.Text =  Reply;
                //string s in org.Split(new string[] { " ", "," }, StringSplitOptions.RemoveEmptyEntries))
                foreach (string subString in Reply.Split(new string[] { " ", "," }, StringSplitOptions.RemoveEmptyEntries))//.Split(new string[] {" " ","},SplitStringOptions.RemoveEmptyEntries))
                {
                    textBox1.Text =textBox1.Text+ " " + Reply;
                }
    
            }
     All I seem to get is three copies of Reply exactly as given the foreach & Split is not splitting as I am expecting it to.  I think all I want to do is get each of the numbers out of a string that contains them for example RE=0,123,456,789 I just want the 0 the 123 etc. as numbers is the foreach / split right for getting them to individual strings that can then be converted to integers with  Convert.To ???

    Or am I heading down the wrong trail with this! 

    Glenn
     
    Monday, October 19, 2009 11:36 AM
  • change textBox1.Text =textBox1.Text+ " " + Reply; to textBox1.Text = textBox1.Text + " " + subString;

    By the way is your code for removing , and spaces?

    If it is you dont have to use split it's slow you can use replace.

    Here is a sample.

    textBox1.Text=Reply.Replace(" ","").Replace(",","")

    Monday, October 19, 2009 11:40 AM
  • It's honestly okay to have the trailing and leading spaces.  When you convert to integer, account for them.

    string someValue = "   15   ";
    int value = int.Parse(someValue, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite);
    Console.WriteLine(value);

    If you can manage to get the numbers even with the spaces, use those couple of flags passed in to int.Parse, and that should do the trick.  You'll get proper numbers.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    Monday, October 19, 2009 11:42 AM
  • Hi Glenn, it is not splitting as expected because in the foreach you are using the Reply instead of subString.

    textBox1.Text = textBox1.Text+ " " + Reply; <-- should be subString

    I always try to Keep it Sharp & simple.
    Monday, October 19, 2009 11:43 AM
  • Hi,

    This getting too odd, I have used similar code as before without problems, the code below was a mod of it



    private void button1_Click(object sender, EventArgs e) { string Reply; Reply = label1.Text; //Reply = " 12, 34 , 56"; MessageBox.Show(Reply); //textBox1.Text = Reply; //string s in org.Split(new string[] { " ", "," }, StringSplitOptions.RemoveEmptyEntries)) foreach (string subString in Reply.Split(new string[] { " ", "," }, StringSplitOptions.RemoveEmptyEntries))//.Split(new string[] {" " ","},SplitStringOptions.RemoveEmptyEntries)) { textBox1.Text = textBox1.Text+" "+ subString; } }
    I am trying to take a string for argument sake RE=0,123,456,789 and split it into the numbers 0, 123, 456 & 789 as strings thinking I could do a Convert.To on them and go, but no! I am thinking right arn't I foreach to split once the they are in serperate strings Convert.To and away.

    Hmm,
    Glenn
     
    Monday, October 19, 2009 11:55 AM
  • I hope this is the code you are looking for.

                string Reply;
                Reply = "   12, 34 ,              56";
                string[] sArr = Reply.Split(new string[] { " ", "," }, StringSplitOptions.RemoveEmptyEntries);
                int[] iArr = new int[sArr.Length];
                int i = 0;
                foreach (string subString in sArr)
                {
                    iArr[i] = Convert.ToInt32(subString);
                    i++;
                }
    • Marked as answer by GlennPatton Monday, October 19, 2009 12:27 PM
    Monday, October 19, 2009 12:01 PM
  • Yes thanks!
    Monday, October 19, 2009 12:28 PM