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

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

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?

图片转代码服务由CSDN问答提供 功能建议

我从字符串包中了解ToUpper和ToLower,但显然它们在这里没有帮助。 有内置功能还是我必须自己写一个?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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)
    }
    
    打赏 评论

相关推荐 更多相似问题