如何将字符串转换为小写形式?

huangapple go评论82阅读模式
英文:

How do I convert a string to a lower case representation?

问题

如何将字符串转换为小写形式?

我觉得肯定有内置的函数可以做到,但我就是找不到。

我在“unicode/letter”中找到了一个“ToLower”,但它只能逐个字符地工作。

英文:

How do I convert a string to a lower case representation?

I feel that there must be built-in function for it, but I just can't find it.

I did find a ToLower in "unicode/letter", but it only works for one rune at a time.

答案1

得分: 172

是的,有的,可以查看字符串包

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.ToLower("Gopher"))
}
英文:

Yes there is, check the strings package.

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.ToLower("Gopher"))
}

答案2

得分: 54

如果你懒得点击链接查看strings包,这里有示例代码:

strings.ToLower("Hello, WoRLd") // => "hello, world"

如果你需要处理阿塞拜疆语或土耳其语等Unicode特殊情况,你可以使用ToLowerSpecial

strings.ToLowerSpecial(unicode.TurkishCase, "Hello, WoRLd") // => "hello, world"
英文:

If you happen to be too lazy to click through to the strings package, here's example code:

strings.ToLower("Hello, WoRLd") // => "hello, world"

If you need to handle a Unicode Special Case like Azeri or Turkish, you can use ToLowerSpecial:

strings.ToLowerSpecial(unicode.TurkishCase, "Hello, WoRLd") // => "hello, world"

huangapple
  • 本文由 发表于 2012年5月2日 18:01:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/10411538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定