如何计算变量中的单词数?

时间:2013-07-09 11:15:18

标签: arrays vb.net

我想要做的是计算一个变量的单词数。这是一个刽子手游戏,将用逗号分隔。所以我基本上希望变量看起来像这样:

"hang,man,group,toll,snail"

我打算用逗号分割它来创建一个数组,但除此之外,我完全迷失了该怎么做。

另一方面,我很高兴看到任何其他建议来整理在刽子手游戏中使用的单词!

5 个答案:

答案 0 :(得分:3)

你已经到了一半。

Dim wordCount as Integer = "hang,man,group,toll,snail".Split(",").Length

将其拆分为数组,然后返回该数组中的元素数。

答案 1 :(得分:2)

Dim Words as String = "hang,man,group,toll,snail"

Dim Word = Words.Split(",")

所以结果将是.. Word(0)=“挂”,Word(1)=“man”......等......

答案 2 :(得分:1)

像这样使用Split

Dim words As String() = SOMESTRING.Split(New Char() {","c})

现在找到你可以

的长度
words.length ' set this to a variable or output

另外,您也可以单独使用单词

words(0)     ' set this to a variable or output
words(1)     ' set this to a variable or output

等等

答案 3 :(得分:0)

您可以使用String.Split方法轻松地将字符串拆分为数组。

有许多重载,但我经常使用的是这一个:

Dim myString as String = "hang,man,group,toll,snail"
Dim myStringArray as String()

myStringArray = myString.Split(new String { "," }, StringSplitOptions.None)

这将为您提供长度为5的字符串数组。

可以在此处找到文档:http://msdn.microsoft.com/en-us/library/tabh47cf.aspx

答案 4 :(得分:0)

Dim Text As String
Dim i As Integer
Dim ary() As String

Text =  "hang,man,group,toll,snail"
ary = Text.Split(",")

For i = 0 To UBound(ary)
    MsgBox(ary(i)) 'here you will get all the words in the array
Next i

MsgBox(i) 'You will get the number of items in your array