locked
how to find multiple numbers in a string that separated with the same marks?? RRS feed

  • Question

  • hello 

    how can i show this three decimal numbers(0.000,0.000,0.000) in three listbox as separately??

    this three decimal number are variable numbers that may increase or decrease .

    <Idle|MPos:0.000,0.000,0.000|FS:0,0|Pn:P>
    
    i used of IndexOf() and Substring() functions for do this but i couldn't  get a good result.

    Friday, April 27, 2018 3:02 PM

Answers

  •             string InputStr = "0.000,0.000,0.000";
                string[] SpiltStr = InputStr.Split(',');
                listBox1.Items.Add(SpiltStr[0]);
                listBox2.Items.Add(SpiltStr[1]);
                listBox3.Items.Add(SpiltStr[2]);
    
    https://msdn.microsoft.com/en-us/library/system.string.split%28v=vs.110%29.aspx
    • Proposed as answer by Fei Hu Monday, April 30, 2018 2:58 AM
    • Marked as answer by ashkan4818 Monday, June 4, 2018 11:54 AM
    Friday, April 27, 2018 3:45 PM
  • Check this example too:

    string text = "<Idle|MPos:1.123,2.456,3.789|FS:0,0|Pn:P>";

    var m = Regex.Match( text, @"\bMPos:(.*?),(.*?),(.*?)\|" );

    if( m.Success )

    {

           string number1 = m.Groups[1].Value;

           string number2 = m.Groups[2].Value;

           string number3 = m.Groups[3].Value;

           //. . .

    }

     

    Then add the numbers to your listboxes.

    • Proposed as answer by Wizend Saturday, April 28, 2018 10:24 AM
    • Marked as answer by ashkan4818 Monday, June 4, 2018 11:54 AM
    Friday, April 27, 2018 5:49 PM
  • Hello ashkan4818,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know. Maybe you could try the below code.

                string str = "<Idle|MPos:0.000,0.000,0.000|FS:0,0|Pn:P>";
                string temp= str.Substring(str.IndexOf(':'));
                string[] result = temp.Substring(0, temp.IndexOf('|')).Split(',');
                string s1 = result[0];
                string s2 = result[1];
                string s3 = result[2];

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ashkan4818 Monday, June 4, 2018 11:54 AM
    Thursday, May 3, 2018 7:15 AM

All replies

  •             string InputStr = "0.000,0.000,0.000";
                string[] SpiltStr = InputStr.Split(',');
                listBox1.Items.Add(SpiltStr[0]);
                listBox2.Items.Add(SpiltStr[1]);
                listBox3.Items.Add(SpiltStr[2]);
    
    https://msdn.microsoft.com/en-us/library/system.string.split%28v=vs.110%29.aspx
    • Proposed as answer by Fei Hu Monday, April 30, 2018 2:58 AM
    • Marked as answer by ashkan4818 Monday, June 4, 2018 11:54 AM
    Friday, April 27, 2018 3:45 PM
  • Check this example too:

    string text = "<Idle|MPos:1.123,2.456,3.789|FS:0,0|Pn:P>";

    var m = Regex.Match( text, @"\bMPos:(.*?),(.*?),(.*?)\|" );

    if( m.Success )

    {

           string number1 = m.Groups[1].Value;

           string number2 = m.Groups[2].Value;

           string number3 = m.Groups[3].Value;

           //. . .

    }

     

    Then add the numbers to your listboxes.

    • Proposed as answer by Wizend Saturday, April 28, 2018 10:24 AM
    • Marked as answer by ashkan4818 Monday, June 4, 2018 11:54 AM
    Friday, April 27, 2018 5:49 PM
  • Hello ashkan4818,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know. Maybe you could try the below code.

                string str = "<Idle|MPos:0.000,0.000,0.000|FS:0,0|Pn:P>";
                string temp= str.Substring(str.IndexOf(':'));
                string[] result = temp.Substring(0, temp.IndexOf('|')).Split(',');
                string s1 = result[0];
                string s2 = result[1];
                string s3 = result[2];

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ashkan4818 Monday, June 4, 2018 11:54 AM
    Thursday, May 3, 2018 7:15 AM