2015-11-11 100 views
-1

我有Visual Basic的這個功課,我明白的代碼是想這樣做,但我不知道該怎麼辦呢字符串和數組

反正這是不完整的代碼

Function GetNextItem(NextLine As String) As String(,) 
    Dim answer(0, 1) As String 
    Dim String1, String2 As String 
    Dim i As Integer = 0 'variable to store what character you are currently looking at 
    String1 = "" 
    String2 = "" 
    'Loop through the string until you hit a comma. Everything before the comma gets added 
one character at a time to String1 


    'Add one more to i so it moves passed the comma 

    'Loop from that character to the end of the string. Anything left gets added to String2 

    'First block of the 1x2 array gets String1 
    'Second block of the 1x2 array gest String2 

    'The 1x2 array is returned 
    Return answer 
End Function 

的說明在評論 我做了一點。我不需要我想要找到一種方法來解決這個問題。 我真的需要這方面的幫助:3

+1

這是VBA還是VB?不同的動物,你說VB,但有VBA標籤。 –

+1

很高興你不想讓我們爲你做你的功課。當您遇到困難時,我們很樂意爲您提供幫助。尋找下一個循環。這是一個起點。 ;-) –

+0

與第一次遇到的逗號相比,您被要求將字符串NextLine減半。 string1包含前面的逗號,string2後面的所有其他內容。兩個字符串以2的數組形式返回 –

回答

0

這是你想要的嗎?

Function GetNextItem(NextLine As String) As String(,) 
    Dim answer(0, 1) As String 
    Dim String1 As String = "", String2 As String = "" 
    Dim i As Integer = 0 'variable to store what character you are currently looking at 
    'Loop through the string until you hit a comma. Everything before the comma gets addedone character at a time to String1 
    For Each character In NextLine 
     i = NextLine.IndexOf(character) 
     If character <> ","c Then 
      String1 += character 
     Else 
      Exit For 
     End If 
    Next 

    'Add one more to i so it moves passed the comma 
    '?????? - I don't think this part is needed 

    'Loop from that character to the end of the string. Anything left gets added to String2 
    Dim counter As Integer = -1 
    For Each character In NextLine 
     ' Can not use NextLine(character) here, because a string can have more than once the same character 
     counter += 1 
     If counter <= i Then Continue For 
     String2 += character 
    Next 

    'First block of the 1x2 array gets String1 
    answer(0, 0) = String1.Trim 

    'Second block of the 1x2 array gest String2 
    answer(0, 1) = String2.Trim 

    'The 1x2 array is returned 
    Return answer 
End Function 
+0

他可以使用ToCharArray嗎?以及如果在達到逗號時切換陣列的邊? :o) –

+0

我不知道,他可以告訴我們他是否接受其他方法或不接受,我這樣說,因爲一旦我的老師否認我的考試,因爲我使用了她沒有教我的功能。 ..這就是爲什麼 –

+0

是不是尷尬,只是有一個笑:) –