2013-07-09 61 views
1

我想要做的是計算一個變量所具有的單詞數量。這是一個hang子手遊戲,並會用逗號分隔。所以我基本上希望變量將是這個樣子:如何計算變量中的單詞數量?

"hang,man,group,toll,snail" 

我打算用逗號分割它創建一個數組,但除此之外,我完全失去了什麼做。

另一方面,我很高興看到任何其他的建議,用於整理在hang子手遊戲中使用的單詞!

回答

3

你在這裏一半。

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

將其拆分成數組,然後返回該數組中的元素數。

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

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

Dim Word = Words.Split(",") 

那麼結果將是..字(0)= 「掛起」,字(1)= 「人」 ......等等..

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 
1

採用分體式像這樣

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

現在找到長度,你可以

words.length ' set this to a variable or output 

alternativly你也可以使用的話單獨

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