Find specific text in RichTextBox and set it to a string c#

后端 未结 4 1128
一向
一向 2021-01-28 13:56

I am at a loss as to how to do this. I am printing some information to a richtextbox that will be multiple lines, has words and numbers. I need to search the richtextbox for spe

4条回答
  •  故里飘歌
    2021-01-28 14:09

    1. Use String.Split to parse the string into an array
    2. Iterate array and use Int32.TrParse method to determine if the "word" is in fact a number

          var input = "User: Matt  User's number: 10";
          int num = 0;
          foreach(var word in input.Split(' '))
          {
              if (Int32.TryParse(word, out num) && Enumerable.Range(1,30).Contains(num))
              {
                  Console.WriteLine("The user number is " + num);
                  break;
              }
          }
      

    or with linq:

            int testNum;
            var digits = input.Split(' ').Where(a => Int32.TryParse(a, out testNum) && Enumerable.Range(1, 30).Contains(testNum)).FirstOrDefault();
            Console.WriteLine("Linq The user number is " + (!string.IsNullOrEmpty(digits) ? digits : "Not Found"));
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题