文字列から数字だけを取得したい。
これが私の文字列であると言わないでください:
324ghgj123
私は手に入れたい:
324123
私が試したこと:
MsgBox(Integer.Parse("324ghgj123"))
Regex
これに使用できます
Imports System.Text.RegularExpressions
次に、コードの一部で
Dim x As String = "123a123&*^*&^*&^*&^ a sdsdfsdf"
MsgBox(Integer.Parse(Regex.Replace(x, "[^\d]", "")))
これを試して:
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
または、文字列が文字の配列であるという事実を利用できます。
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
resultString = Regex.Match(subjectString, @"\d+").Value;
その番号を文字列として提供します。Int32.Parse(resultString) は、番号を提供します。
線形検索アプローチでは、このアルゴリズムを使用できます。これは 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);
}