VB.Net 从字符串函数中提取数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14064468/
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
VB.Net Extract numbers from string function
提问by Alex
My request is one that can extract a number somewhat by a search.
我的要求是可以通过搜索在一定程度上提取一个数字。
Example:
animalsOwned|4would return an containing "4"
示例:
animalsOwned|4将返回一个包含“4”的
animals|3|2|1|3would return an array containing "3", "2", "1", "3"
animals|3|2|1|3将返回一个包含“3”、“2”、“1”、“3”的数组
This would make it easier for me during a file stream reader. Thank you
这将使我在文件流阅读器期间更容易。谢谢
回答by Inisheer
Dim astring = "ABCDE|1|2|3|4"
Dim numbers = (From s In astring
Where Char.IsDigit(s)
Select Int32.Parse(s)).ToArray()
This LINQ statement should help. It simply checks each character in a string to see if it's a digit. Note that this only applies to single digit numbers. It becomes a bit more complicated if you want "ABC123" to return 123 vs. 1, 2, 3 array.
这个 LINQ 语句应该会有所帮助。它只是检查字符串中的每个字符以查看它是否是数字。请注意,这仅适用于单个数字。如果您希望 "ABC123" 返回 123 与 1, 2, 3 数组,则会变得有点复杂。
回答by Richard Dong
Try regular expression. It's a powerful tool for simple text parsing.
试试正则表达式。它是用于简单文本解析的强大工具。
Imports System.Text.RegularExpressions
Namespace Demo
Class Program
Shared Function Main(ByVal args As String()) As Integer
Dim array As Integer() = ExtractIntegers("animals|3|2|1|3")
For Each i In array
Console.WriteLine(i)
Next
Return 0
End Function
Shared Function ExtractIntegers(ByVal input As String) As Integer()
Dim pattern As String = "animals(\|(?<number>[0-9]+))*"
Dim match As Match = Regex.Match(input, pattern)
Dim list As New List(Of Integer)
If match.Success Then
For Each capture As Capture In match.Groups("number").Captures
list.Add(Integer.Parse(capture.Value))
Next
End If
Return list.ToArray()
End Function
End Class
End Namespace
回答by Taicho
You can do a variable.Split("|")and then assign each piece to an array level.
您可以执行 avariable.Split("|")然后将每个部分分配给一个数组级别。
You can do a count on string and with a while or for loop, you can assign the splited sections to array levels. Then you can do a IsNumeric()check for each array level.
您可以对字符串进行计数,并使用 while 或 for 循环,您可以将拆分的部分分配给数组级别。然后您可以IsNumeric()对每个数组级别进行检查。
回答by kewltek
I haven't programmed VB for awhile but I'll give you some pseudo code: First, loop through each line of file. Call this variable Line. Then, take the index of what you're searching for: like Line.indexOf("animalsOwned") If it returns -1 it isn't there; continue. Once you find it, add the Index variable to the length of the search string and 1. (Index=Index+1+Len(searchString)) Then, take a substring starting there, and end at the end of the line. Explode the substring by | characters, then add each into an array. Return the array.
我已经有一段时间没有编写 VB 了,但我会给你一些伪代码:首先,循环遍历文件的每一行。调用这个变量 Line。然后,获取您要搜索的内容的索引:例如 Line.indexOf("animalsOwned") 如果它返回 -1,则它不存在;继续。找到后,将 Index 变量添加到搜索字符串的长度和 1。 (Index=Index+1+Len(searchString)) 然后,从那里开始取一个子字符串,并在行尾结束。用 | 分解子串 字符,然后将每个字符添加到数组中。返回数组。
Sorry that I can't give you much help, but I'm working on an important PHP website right now ;).
抱歉,我不能给你太多帮助,但我现在正在一个重要的 PHP 网站上工作;)。

