vb.net vb. net 将一个巨大的字符串拆分为单词并获得(单词)索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20884622/
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 split a huge string into words and get a (word) index
提问by abou_hafs
I have a huge text file in arabic .. i want to search about any sentence and if i found it get the index of the first word .. i repeat (word) not character .. how to do this please ?
我有一个巨大的阿拉伯语文本文件。
for example:
例如:
Dim myString as String = "Fundamentally programs manipulate numbers and text. These are the building blocks of all programs. Programming languages let you use them in different ways, eg adding numbers, etc, or storing data on disk for later retrieval"
so.. when i search about (programs) i want to return: 1 and 13 .. any suggestions by the best way ? thanx
所以.. 当我搜索(程序)时,我想返回:1 和 13 .. 有什么最好的建议吗?谢谢
回答by Heinzi
First you split the string into words. We will use this to conveniently remove punctuation as well (expand as necessary):
首先,您将字符串拆分为单词。我们也将使用它来方便地删除标点符号(根据需要进行扩展):
Dim words = myString.Split({". ", ", ", " "}, StringSplitOptions.None)
Then you search for the word in question (case-insensitive):
然后搜索相关单词(不区分大小写):
Dim indexes = From i In Enumerable.Range(0, words.Length)
Where String.Equals(words(i), "programs", StringComparison.CurrentCultureIgnoreCase)
Select i
Then you output the result (optional):
然后输出结果(可选):
For Each i In indexes
Console.WriteLine(i)
Next
回答by user2919973
Dim line As String = "hello world"
Dim words As String() = line.Split(New Char() {" "}, StringSplitOptions.RemoveEmptyEntries)
words(index)
or
或者
Dim line As String = "hello world"
Dim words As String() = line.Split(New Char() {" "}, StringSplitOptions.RemoveEmptyEntries)(index)
回答by Sergi
You can use the Split function:
您可以使用Split function:
Dim array() As String = Split(myString, " ")
In case you want to split by spaces. Then if you want to get the n position just use array(n-1).
如果您想按空格分割。然后,如果您想获得 n 位置,只需使用array(n-1).

