22

文字列から数字だけを取得したい。

これが私の文字列であると言わないでください:

324ghgj123

私は手に入れたい:

324123

私が試したこと:

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

8 に答える 8

39

Regexこれに使用できます

Imports System.Text.RegularExpressions

次に、コードの一部で

Dim x As String = "123a123&*^*&^*&^*&^   a sdsdfsdf"
MsgBox(Integer.Parse(Regex.Replace(x, "[^\d]", "")))
于 2012-11-13T05:37:38.383 に答える
24

これを試して:

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
于 2012-11-13T05:35:16.617 に答える
6

または、文字列が文字の配列であるという事実を利用できます。

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
于 2012-11-13T05:43:01.147 に答える
2
resultString = Regex.Match(subjectString, @"\d+").Value;

その番号を文字列として提供します。Int32.Parse(resultString) は、番号を提供します。

于 2012-11-13T05:45:53.057 に答える
0

線形検索アプローチでは、このアルゴリズムを使用できます。これは 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);
}
于 2012-11-13T05:50:59.927 に答える