将字符从小写转换为大写,反之亦然 VB.Net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21334274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:33:31  来源:igfitidea点击:

Converting characters from Lower to upper case and vice versa VB.Net

vb.netstringuppercaselowercase

提问by James Sowter

had a search around and can't find an answer.

四处搜索,找不到答案。

I've been tasked with converting a strings capitalization from whatever it is in Be it lower case or upper case and swap them round..

我的任务是将字符串大写转换为小写或大写,然后将它们轮换。

For Example :- Input :- "HeLlO" and Output :- "hElLo"

例如:-输入:-“HeLlO”和输出:-“hElLo”

I understand that i need to use a for loop but have not been able to figure out how to step through each character, check the case and switch it if needs be.

我知道我需要使用 for 循环,但无法弄清楚如何遍历每个字符,检查大小写并在需要时切换它。

I can make a for loop that counts through and displays the individual characters or a simple If statement to convert the whole string into Upper or lower but if i try to combine the 2 my logic isn't working right.

我可以创建一个 for 循环来计算并显示单个字符或一个简单的 If 语句来将整个字符串转换为 Upper 或 lower ,但是如果我尝试将 2 组合起来,我的逻辑就无法正常工作。

Can anyone help at all?

任何人都可以帮忙吗?

回答by Steven Doggart

Here is one simple way to do it:

这是一种简单的方法:

Public Function InvertCase(input As String) As String
    Dim output As New StringBuilder()
    For Each i As Char In input
        If Char.IsLower(i) Then
            output.Append(Char.ToUpper(i))
        ElseIf Char.IsUpper(i) Then
            output.Append(Char.ToLower(i))
        Else
            output.Append(i)
        End If
    Next
    Return output.ToString()
End Function

It just loops through each character in the original string, checks to see what case it is, fixes it, and then appends that fixed character to a new string (via a StringBuilderobject).

它只是遍历原始字符串中的每个字符,检查它是什么情况,修复它,然后将该固定字符附加到新字符串(通过StringBuilder对象)。

As Neolisk suggested in the comments below, you could make it cleaner by creating another method which converts a single character, like this:

正如 Neolisk 在下面的评论中建议的那样,您可以通过创建另一种转换单个字符的方法来使其更清晰,如下所示:

Public Function InvertCase(input As Char) As Char
    If Char.IsLower(input) Then Return Char.ToUpper(input)
    If Char.IsUpper(input) Then Return Char.ToLower(input)
    Return input
End Function

Public Function InvertCase(input As String) As String
    Dim output As New StringBuilder()
    For Each i As Char In input
        output.Append(InvertCase(i))
    Next
    Return output.ToString()
End Function

Using that same function for InvertCase(Char), you could also use LINQ, like this:

对 使用相同的函数InvertCase(Char),您还可以使用 LINQ,如下所示:

Public Function InvertCase(input As String) As String
    Return New String(input.Select(Function(i) InvertCase(i)).ToArray())
End Function

回答by Damien_The_Unbeliever

As a Linq query:

作为 Linq 查询:

Dim input = "HeLlO"
Dim output = new String(input.Select(Function(c)
                            Return If(Char.IsLower(c),Char.ToUpper(c),Char.ToLower(c))
                        End Function).ToArray())
Console.WriteLine(output)

Honestly, who writes loops these days? :-)

老实说,现在谁写循环?:-)