2015-03-13 71 views
0

假設我需要將單詞「hello」中每個字符的ASCII版本添加到「hi」,以便結果如下所示:(h + h =) (e + i =)(l + h =)(l + i =)(o + h =)等等我將如何去循環「hi」字符串? (h + h =)(h + i =)(e + h =)(e + i = 0)我已經設法循環使用「hello」字符串,但不太清楚如何做第二次, )等。VB.NET將字符串循環到一組數字

謝謝!

+0

你可以告訴你試過嗎?也許這也會讓問題更清楚。 – 2015-03-13 09:21:47

回答

1

您可以使用Mod操作員重新開始索引。例如:

Dim str1 as String = "hello" 
Dim str2 as String = "hi" 

' This gets the length of the longest string 
Dim longest = Math.Max(str1.Length, str2.Length) 

' This loops though all characters 
' The Mod operator makes the index wrap over for the shorter string 
For i As Integer = 0 To longest - 1 
    Console.Write(str1(i Mod str1.Length)) 
    Console.WriteLine(str2(i Mod str2.Length)) 
Next 

輸出:

hh 
ei 
lh 
li 
oh 
+0

謝謝,這是完美的! – aywowimsostuck 2015-03-13 09:34:48

相關問題