dongshuo8756 2016-07-06 21:25
浏览 184
已采纳

如何反转字符串中每个字母的大小写?

I know about ToUpper and ToLower from strings package but obviously they won't help here. Is there a built-in function or do I have to write one myself?

  • 写回答

1条回答 默认 最新

  • dongtang1966 2016-07-06 21:34
    关注

    You need to write one yourself, but the building blocks are already in the standard library:

    func swapCase(s string) string {
        return strings.Map(func(r rune) rune {
            switch {
            case unicode.IsLower(r):
                return unicode.ToUpper(r)
            case unicode.IsUpper(r):
                return unicode.ToLower(r)
            }
            return r
        }, s)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?