我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

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

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?

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)
}

近期文章

  • 什么是实现实体组件系统的最佳方法
  • 如何在接口上调用len()?
  • 接受图片标题请求
  • GO-Github API:如何提交并推送到远程分支
  • 将JSON对象的字符串编组到JSON数组中
  • 顺利处理运行时的紧急情况
  • 由于缺少包含路径,因此无法构建使用Swig构建的go模块
  • 如何触发Java应用程序(来自Google GO / golang子进程的java -jar <app name>
  • 如何通过reflect.Append将nil附加到动态类型切片
  • 测试Go中从Function返回的Cookie
  • Golang中的模糊字符串匹配
  • 我应该如何在后台运行Golang进程?
  • 代表许多领域的最优雅的方式
  • 带有mgo驱动程序的MongoDB管理员命令
  • Go-可变参数函数传递
  • Golang mgo MongoDB bson.ObjectId非utf-8错误
  • 在Go中封装结构
  • 尝试异常替代方案以保护应用程序不崩溃
  • 接口内部的反射数组
  • go 1.5 trace命令

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华