none
C#Split()函数得使用 RRS feed

  • 问题

  • 我需要从以下格式的字符串中提取数字,请问我该如何操作?

    串口接收到的数据:

    显示状态:

    accx:12.0

    accy:13.0

    accz:14.0

    实际的字符串状态“accx:12.0/r/taccy:13.0/r/taccz:14.0/r/t”

    需要的结果

    将12.0 13.0 14.0提取出来。

    2020年2月15日 14:04

答案

  • 建议你用正则表达式好一点:

      string s = "accx: 12.0 / r / taccy:13.0 / r / taccz:14.0 / r / t";
                Regex reg = new Regex("(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?");
                foreach(var num in reg.Matches(s))
                {
                    Console.WriteLine(num);
                }


    Reproduce your quesions with ScreenToGif is your choice. 
    For IIS: IIS Forum
    For WebSite of .NET: ASP.NET Forum
    For others: StackExchange
    For spam-sender or forum urgent issues, Send your Email at:  forumsfeedback@microsoft.com

    • 已标记为答案 Doemni 2020年2月24日 18:13
    2020年2月16日 9:10
    版主
  • Hi Doemni,

    你可以采用下面的方法来分离字符串。

     static void Main(string[] args)
            {
                string str = "accx:12.0/r/taccy:13.0/r/taccz:14.0/r/t";
                string[] arr = { "/r/t" };
                var result = str.Split(arr, StringSplitOptions.RemoveEmptyEntries).ToList().Select(r => r.Remove(0,5));
                foreach (var item in result)
                {
                    Console.WriteLine(item);
                }
    
                Console.ReadKey();
    
            }

    Best Regards,

    Jack


    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.

    • 已标记为答案 Doemni 2020年2月24日 18:13
    2020年2月17日 2:43
    版主

全部回复

  • 建议你用正则表达式好一点:

      string s = "accx: 12.0 / r / taccy:13.0 / r / taccz:14.0 / r / t";
                Regex reg = new Regex("(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?");
                foreach(var num in reg.Matches(s))
                {
                    Console.WriteLine(num);
                }


    Reproduce your quesions with ScreenToGif is your choice. 
    For IIS: IIS Forum
    For WebSite of .NET: ASP.NET Forum
    For others: StackExchange
    For spam-sender or forum urgent issues, Send your Email at:  forumsfeedback@microsoft.com

    • 已标记为答案 Doemni 2020年2月24日 18:13
    2020年2月16日 9:10
    版主
  • Hi Doemni,

    你可以采用下面的方法来分离字符串。

     static void Main(string[] args)
            {
                string str = "accx:12.0/r/taccy:13.0/r/taccz:14.0/r/t";
                string[] arr = { "/r/t" };
                var result = str.Split(arr, StringSplitOptions.RemoveEmptyEntries).ToList().Select(r => r.Remove(0,5));
                foreach (var item in result)
                {
                    Console.WriteLine(item);
                }
    
                Console.ReadKey();
    
            }

    Best Regards,

    Jack


    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.

    • 已标记为答案 Doemni 2020年2月24日 18:13
    2020年2月17日 2:43
    版主