2015-03-13 63 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

相关问题