[英]Having trouble splitting a PascalCase string and storing it in an array in VB
CorrectHorseBatteryStaple
至
Correct
Horse
Battery
Staple
(Empty)
還有一個問題,除了Mid(),Right(),Left(),Len(),Asc(),我不能使用類,函數,內置函數。 這使整個事情變得更加困難。
我一輩子都想不通如何比較字符串中的字符,以某種方式停止循環/將第一個單詞存儲在數組中,依此類推。
到目前為止,這是我所做的,沒有任何意義:
Sub Main()
Dim input As String
Dim str(5) As String
Dim tempstr As String
Dim temp As Char
Dim temp2 As Char
Dim l As Integer
Console.WriteLine("Enter the string: ")
input = Console.ReadLine()
l = Len(input)
For z As Integer = 1 To 5
For i As Integer = 1 To l
temp = Mid(input, i, l)
temp2 = Mid(input, i + 1, l)
If Asc(temp) > 65 And Asc(temp) < 90 Then
tempstr = temp
If Asc(temp2) > 65 And Asc(temp2) < 90 Then
tempstr = temp
Else
tempstr = tempstr & temp
End If
Else
tempstr = tempstr & temp
End If
Next i
str(z) = tempstr
Next z
For a As Integer = 1 To 5
Console.WriteLine(str(a))
Next a
Console.ReadKey()
End Sub
顯然,您還沒有空運行代碼。 它到處都是錯誤,因此它永遠不會按預期運行。
Dim str(5) As String
For z As Integer = 1 To 5 ' will never run for over 5 words
在下一行中,我認為您打算使用Mid(input, i , 1)
。 1
不是l
。 l
會給你整個字符串,而不僅僅是一個字母。
temp = Mid(input, i, l)
temp2 = Mid(input, i + 1, l)
這條線不會考慮A
和Z
您應該使用>=
和<=
If Asc(temp) >= 65 And Asc(temp) <= 90 Then
該行將在最后一個字符上返回錯誤或空字符串
temp2 = Mid(input, i + 1, l)
該行將不考慮數組中的第一個元素
For a As Integer = 1 To 5
Console.WriteLine(str(a))
Next a
盡管VB.net的功能可以幫助您更清晰,更少地編寫代碼,但看起來似乎受限於使用本機VB6函數的要求。
下面的代碼,再次限制為5個字,應該為您提供所需的輸出:
Sub Main()
Dim input As String
Dim str(5) As String
Dim tempstr As String
Dim temp As Char
Dim temp2 As Char
Dim l As Integer
Dim arrCounnter As Integer
Console.WriteLine("Enter the string: ")
input = Console.ReadLine()
tempstr = ""
l = Len(input)
For i As Integer = 1 To l
temp = Mid(input, i, 1)
'If capital, add to new temp; put old temp in array
If Asc(temp) >= 65 And Asc(temp) <= 90 Then
If tempstr <> "" Then
str(arrCounnter) = tempstr
arrCounnter = arrCounnter + 1
End If
tempstr = temp
Else
'If not, add to old temp, nxt
tempstr = tempstr & temp
End If
If i = l Then str(arrCounnter) = tempstr
Next i
For a As Integer = 0 To 5
If str(a) = "" Then
Console.WriteLine("(Empty)")
Else
Console.WriteLine(str(a))
End If
Next a
Console.ReadKey()
End Sub
在開始之前,我建議您使用列表而不是數組。 這樣,如果您想分割更多的單詞,則無需更改代碼。 但是,我想您還沒有涵蓋這些內容。 所以...
最簡單的方法是遍歷數組的每個字符,如果該字符是大寫字母,則繼續進行下一個數組項並將該字符添加到數組項。 如果字符是小寫字母,則只需將其添加到當前數組項中即可。 您無需通過這種方式使用太多變量。
此處假設第一個字母為大寫。 如果不是,將會有一個
索引超出范圍
錯誤。
干得好 ..
Module module1
Sub Main()
Dim input As String
Dim str(3) As String
Dim temp As String
Dim l As Integer
Dim z As Integer = -1 ' array index
Console.WriteLine("Enter the string: ")
input = Console.ReadLine()
l = Len(input)
For i As Integer = 1 To l
temp = Mid(input, i, 1)
'if temp is a capital letter increase the array index by 1 and add temp to that array item
If (Asc(temp) >= 65 And Asc(temp) <= 90) Then
z = z + 1
str(z) = str(z) & temp
End If
' if the temp is lower case then just add temp to the current array item
If (Asc(temp) >= 97 And Asc(temp) <= 122) Then
str(z) = str(z) & temp
End If
Next
Console.WriteLine()
For a As Integer = 0 To 3
Console.WriteLine(str(a))
Next a
Console.ReadKey()
End Sub
End Module
我應該解釋為什么Z以-1開始。 這是基於以下假設:輸入字符串的第一個字母為大寫。
第一次循環時,存儲在temp
的第一個字符為大寫字母,並且執行了第一個If
語句的內容,因此將1添加到z使得z = 0。 然后,將大寫的第一個字母添加到數組的第一個元素str(0)中。
在進行循環時,后續的小寫字母僅會添加到str(0)中。
當循環到達下一個大寫字母時,將1再次添加到z
,以便z = 1並將大寫字母添加到z(1),依此類推。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.