VB如何输入字符串转换为字符的上下

时间:2014-04-14 14:03:38

标签: vb.net

在VB中如何输入字符串转换为字符upper和lower作为输入'hello' 但输出像'HeLlO' 我的代码thiis

  Sub ConvertCase()
        Dim i As Integer = 0
        Dim inputVal As String = TextBox1.Text
        Dim out As String = Label1.Text
        Do While i > inputVal.Length()
            If inputVal.Length() Mod 2 = 0 Then
                out = inputVal.Substring(0, 1).ToUpper
            End If
            If inputVal.Length() Mod 2 <> 0 Then
                out = inputVal.Substring(1).ToLower
            End If
            Loop
        Label1.Text &= out
    End Sub

我不知道什么是错的&gt;&lt; 谢谢

3 个答案:

答案 0 :(得分:1)

你快到了。你只需要附加

out &= ...

子字符串没有正确完成。

    For i As Integer = 0 To inputVal.Length - 1
        If (i Mod 2) = 0 Then
            out &= Char.ToUpper(inputVal(i))
        Else
            out &= Char.ToLower(inputVal(i))
        End If
    Next

答案 1 :(得分:1)

对于LINQ爱好者:

Label1.Text = New String(TextBox1.Text.Select(Function(c, i) If(i Mod 2 = 0, Char.ToUpper(c), Char.ToLower(c))).ToArray)

答案 2 :(得分:0)

试试这个..

Dim str As String = "hello"
Dim strlength As Integer = str.Length
Dim ii As Integer = 0
Dim output As String = ""
   For Each c As Char In str
     If ii Mod 2 = 0 Then
         output += c.ToString.ToUpper
     Else
         output += c.ToString.ToLower
     End If
     ii += 1
    Next
MsgBox(output)