[英]Get only numbers from string
i want to get only numbers from string.我只想从字符串中获取数字。
Lest say that this is my string:以免说这是我的字符串:
324ghgj123
i want to get:我想得到:
324123
what i have tried:我试过的:
MsgBox(Integer.Parse("324ghgj123"))
you can use Regex
for this 你可以使用正则
Regex
Imports System.Text.RegularExpressions
then on some part of your code 然后在你的代码的某些部分
Dim x As String = "123a123&*^*&^*&^*&^ a sdsdfsdf"
MsgBox(Integer.Parse(Regex.Replace(x, "[^\d]", "")))
try this: 试试这个:
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
or: 要么:
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
Or you can use the fact that a String is an Array of Chars. 或者您可以使用String是Chars数组的事实。
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;
will give you that number as a string. 会把这个数字作为字符串给你。 Int32.Parse(resultString) will then give you the number.
然后Int32.Parse(resultString)将为您提供数字。
You can actually combine some of these individual answers to create a single line solution that will either return an integer with the value 0, or an integer that is the concatenation of all of the numbers in the string. 实际上,您可以组合这些单独的答案中的一些来创建单行解决方案,该解决方案将返回值为0的整数,或者是字符串中所有数字的串联的整数。 Not sure how useful that is, however -- this began as a method to create a string of numbers only....
然而,不确定它是多么有用 - 这开始只是一种创建数字串的方法....
Dim TestMe = CInt(Val(New Text.StringBuilder((From ch In "123abc123".ToCharArray Where IsNumeric(ch)).ToArray).ToString))
For a linear search approach you can use this algorithm, it's in C# but can easily be translated in vb.net, hope it helps. 对于线性搜索方法,您可以使用此算法,它在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);
}
Function that returns string without digits 返回没有数字的字符串的函数
Public Function _RemoveNonDigitCharacters(S As String) As String
Return New String(S.Where(Function(x As Char) System.Char.IsDigit(x)).ToArray)
End Function
str="something1234text456"
Set RegEx = CreateObject("vbscript.regexp")
RegEx.Pattern = "[^\d+]"
RegEx.IgnoreCase = True
RegEx.Global = True
numbers=RegEx.Replace(str, "")(0)
msgbox numbers
Dim a As String = dr("hah34gnb98").ToString()暗淡 As String = dr("hah34gnb98").ToString()
Dim reg As New Regex("[^0-9]")
a = reg.Replace(a, "") : TextBox1.Text = a
string input = "Hello 20, I am 30 and he is 40"; string input = "你好 20,我 30 岁,他 40 岁"; var numbers = Regex.Matches(input, @"\d+").OfType().Select(m => int.Parse(m.Value)).ToArray();
var numbers = Regex.Matches(input, @"\d+").OfType().Select(m => int.Parse(m.Value)).ToArray();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.