【问题标题】:Get only numbers from string仅从字符串中获取数字
【发布时间】:2012-11-01 14:28:20
【问题描述】:

我只想从字符串中获取数字。

免得说这是我的字符串:

324ghgj123

我想得到:

324123

我尝试过的:

MsgBox(Integer.Parse("324ghgj123"))

【问题讨论】:

    标签: vb.net winforms


    【解决方案1】:
    str="something1234text456"   
    
    Set RegEx = CreateObject("vbscript.regexp")
    RegEx.Pattern = "[^\d+]"
    RegEx.IgnoreCase = True
    RegEx.Global = True
    numbers=RegEx.Replace(str, "")(0)
    msgbox numbers
    

    【讨论】:

    • 对于 VBs 其测试解决方案
    【解决方案2】:

    返回不带数字的字符串的函数

    Public Function _RemoveNonDigitCharacters(S As String) As String
            Return New String(S.Where(Function(x As Char) System.Char.IsDigit(x)).ToArray)
    End Function
    

    【讨论】:

      【解决方案3】:

      实际上,您可以将其中一些单独的答案组合起来创建一个单行解决方案,该解决方案将返回一个值为 0 的整数,或者返回一个整数,该整数是字符串中所有数字的串联。然而,不确定它有多大用处——这开始是一种仅创建一串数字的方法....

          Dim TestMe = CInt(Val(New Text.StringBuilder((From ch In "123abc123".ToCharArray Where IsNumeric(ch)).ToArray).ToString))
      

      【讨论】:

        【解决方案4】:

        对于线性搜索方法,您可以使用此算法,它在 C# 中,但可以在 vb.net 中轻松翻译,希望对您有所帮助。

        string str = “123a123”;
        
        for(int i=0;i<str.length()-1;i++)
        {
            if(int.TryParse(str[i], out nval))
                continue;
            else
                str=str.Rremove(i,i+1);
        }
        

        【讨论】:

          【解决方案5】:

          或者您可以使用字符串是字符数组这一事实。

          Public Function getNumeric(value As String) As String
              Dim output As StringBuilder = New StringBuilder
              For i = 0 To value.Length - 1
                  If IsNumeric(value(i)) Then
                      output.Append(value(i))
                  End If
              Next
              Return output.ToString()
          End Function
          

          【讨论】:

            【解决方案6】:
            resultString = Regex.Match(subjectString, @"\d+").Value;
            

            会给你那个数字作为一个字符串。 Int32.Parse(resultString) 然后会给你数字。

            【讨论】:

              【解决方案7】:

              您可以为此使用Regex

              Imports System.Text.RegularExpressions
              

              然后在你的代码的某些部分

              Dim x As String = "123a123&*^*&^*&^*&^   a sdsdfsdf"
              MsgBox(Integer.Parse(Regex.Replace(x, "[^\d]", "")))
              

              【讨论】:

              • 所以你的正则表达式读取:用空字符串替换每个非数字字符。优雅。
              【解决方案8】:

              试试这个:

              Dim mytext As String = "123a123"
              Dim myChars() As Char = mytext.ToCharArray()
              For Each ch As Char In myChars
                   If Char.IsDigit(ch) Then
                        MessageBox.Show(ch)
                   End If
              Next
              

              或:

              Private Shared Function Num(ByVal value As String) As Integer
                  Dim returnVal As String = String.Empty
                  Dim collection As MatchCollection = Regex.Matches(value, "\d+")
                  For Each m As Match In collection
                      returnVal += m.ToString()
                  Next
                  Return Convert.ToInt32(returnVal)
              End Function
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2019-02-02
                • 2013-05-16
                • 1970-01-01
                • 1970-01-01
                • 2021-07-18
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多