vb.net 只从字符串中获取数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13356018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get only numbers from string
提问by Nh123
i want to get only numbers from string.
我只想从字符串中获取数字。
Lest say that this is my string :
免得说这是我的字符串:
324ghgj123
324ghgj123
i want to get:
我想得到:
324123
what i have tried:
我试过的:
MsgBox(Integer.Parse("324ghgj123"))
回答by John Woo
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]", "")))
回答by famf
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
回答by Mark Hall
Or you can use the fact that a String is an Array of 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
回答by Suraj
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) 然后会给你这个数字。
回答by jinzai
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. Not sure how useful that is, however -- this began as a method to create a string of numbers only....
您实际上可以将这些单独的答案中的一些组合起来,以创建一个单行解决方案,该解决方案将返回一个值为 0 的整数,或者一个整数,该整数是字符串中所有数字的串联。然而,不确定这有多大用处——这最初是作为一种仅创建一串数字的方法......
Dim TestMe = CInt(Val(New Text.StringBuilder((From ch In "123abc123".ToCharArray Where IsNumeric(ch)).ToArray).ToString))
回答by Dariusz
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
回答by Sandip Kolawale
str="something1234text456"
Set RegEx = CreateObject("vbscript.regexp")
RegEx.Pattern = "[^\d+]"
RegEx.IgnoreCase = True
RegEx.Global = True
numbers=RegEx.Replace(str, "")(0)
msgbox numbers
回答by SajjadHashmi
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);
}