【问题标题】:how get integer only and remove all string in C# [duplicate]如何仅获取整数并删除 C# 中的所有字符串 [重复]
【发布时间】:2011-06-15 17:48:25
【问题描述】:

如何删除字符串并只获取整数?

我有一个字符串 (01 - ABCDEFG)

我只需要得到 (01)

【问题讨论】:

    标签: c# string integer


    【解决方案1】:
    input = Regex.Replace(input, "[^0-9]+", string.Empty);
    

    【讨论】:

      【解决方案2】:

      有四种不同的方法(可能更多,但我选择了这些)。

      #1:Bala R 的正则表达式

      string output = Regex.Replace(input, "[^0-9]+", string.Empty);
      

      #2:来自 Donut 和 agent-j 的正则表达式

      string output = Regex.Match(input, @"\d+").Value;
      

      #3:Linq

      string output = new string(input.ToCharArray().Where(c => char.IsDigit(c)).ToArray());
      

      #4:子字符串,为了使这个工作,破折号必须在数字和文本之间的字符串中。

      string output = input.Substring(0, input.IndexOf("-")).Replace(" ", "");
      

      使用这些输入:

      string input1 = "01 - ABCDEFG";
      string input2 = "01 - ABCDEFG123";
      

      对于 1 和 2,结果将是:

      output1 = "01";
      output2 = "01123";
      

      对于 3 和 4,结果将是:

      output1 = "01";
      output2 = "01";
      

      如果预期结果是获取字符串中的所有数字,请使用 #1 或 #2,但如果预期结果是仅获取破折号之前的数字,请使用 #3 或 #4。

      如果字符串这么短,#1 和 #2 在速度上大致相等,对于 #3 和 #4 也是如此,但如果有很多迭代或字符串长四倍或更多,则 #2 比#1 和 #4 比 #3 快。

      注意:如果括号包含在字符串 #4 中,则必须修改为此,但这不会使其变慢:

      string output = input.Substring(0, input.IndexOf("-")).Replace(" ", "").Replace("(", "");
      

      【讨论】:

        【解决方案3】:

        试试这个:

        Convert.ToInt32(string.Join(null, System.Text.RegularExpressions.Regex.Split(s, "[^\\d]")))
        

        它返回整数值 1。

        【讨论】:

          【解决方案4】:

          从字符串中获取所有数字

          string str = "01567438absdg34590";
                      string result = "";
          
                       result = Regex.Replace(str, @"[^\d]", "");
          
                      Console.WriteLine(result);
                      Console.Read();
          

          输出:0156743834590

          【讨论】:

            【解决方案5】:
            string snum = System.Text.RegularExpression.Regex.Match(s, "\d+").Value;
            int num;
            if (!int.TryParse(snum, out num))
              throw new Exception();
            

            【讨论】:

              【解决方案6】:

              您应该使用正则表达式——它们是一种非常强大的方法,可以将文本字符串与某些模式进行匹配,这是使用它们的绝佳场景。

              模式“\d+”将匹配 1 个或多个数字的序列。使用此模式从字符串中提取所有数字的简单方法如下:

              public static List<int> ExtractInts(string input)
              {
                 return Regex.Matches(input, @"\d+")
                    .Cast<Match>()
                    .Select(x => Convert.ToInt32(x.Value))
                    .ToList();
              }
              

              所以你可以这样使用它:

              List<int> result = ExtractInts("( 01 - ABCDEFG )");
              

              有关正则表达式的更多详细信息,请参阅this page (MSDN)this page (helpful "cheat sheet")

              【讨论】:

                【解决方案7】:

                查看这篇博文: http://weblogs.asp.net/sushilasb/archive/2006/08/03/How-to-extract-numbers-from-string.aspx

                一个使用正则表达式从字符串中提取数字的简单程序。

                class Program
                    {
                    static void Main(string[] args)
                    {
                      Console.WriteLine(ExtractNumbers("( 01 - ABCDEFG )"));    // 01
                      Console.ReadLine();
                    }
                
                    static string ExtractNumbers(string expr)
                    {
                        return string.Join(null, System.Text.RegularExpressions.Regex.Split(expr, "[^\\d]"));
                    }
                }
                

                希望这会有所帮助!

                【讨论】:

                  【解决方案8】:
                  Response.Write(Convert.ToDecimal(string.Join(null, System.Text.RegularExpressions.Regex.Split(TextBox1.Text, "[^\\d .]"))));
                  

                  这适用于intdecimal 号码。

                  【讨论】:

                    【解决方案9】:

                    试试这个:

                    string XYZ = string.Join(null, System.Text.RegularExpressions.Regex.Split(“XYZ  77”, “[^\d]”));
                    

                    【讨论】:

                      【解决方案10】:

                      你也可以用这种方式得到你合适的答案..

                      string sentence = "10 cats, 20 dogs, 40 fish and 1 programmer";
                       string[] digits= Regex.Split(sentence, @"\D+");
                                   foreach (string value in digits)
                                   {
                                       int number;
                                       if (int.TryParse(value, out number))
                                       {
                                           Debug.Log(value);
                                       }
                                   }
                      

                      【讨论】:

                        猜你喜欢
                        • 2021-11-07
                        • 1970-01-01
                        • 1970-01-01
                        • 2015-01-26
                        • 1970-01-01
                        • 2020-04-15
                        • 2015-06-07
                        • 2011-10-21
                        • 1970-01-01
                        相关资源
                        最近更新 更多