【问题标题】:VB.NET Looping a String to a set NumberVB.NET 将字符串循环到一组数字
【发布时间】:2015-03-13 09:20:25
【问题描述】:
假设我需要将单词“hello”中每个字符的 ASCII 版本添加到“hi”,这样结果就会是这样的: (h+h = )(e+i = )(l+h = )(l+i = )(o+h = ) 等我将如何循环“hi”字符串?
我已经设法循环了“hello”字符串,但不太确定如何在没有得到 (h+h = )(h+i = )(e+h = )(e+i = ) 的情况下执行第二个等等。
谢谢!
【问题讨论】:
标签:
vb.net
string
loops
encoding
character
【解决方案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