Go 中如何检查字符串是全大写还是小写?

Golang 中检查字符串中所有字符是大写还是小写的简单方法是什么?


另外,如何处理字符串有标点符号的情况?


请参阅这些示例:


package main


import (

    "fmt"

    "unicode"

)


func main() {

    s := "UPPERCASE"

    fmt.Println(s.IsUpper())  // Should print true


    s = "lowercase"

    fmt.Println(s.IsUpper())  // Should print false


    s = "lowercase"

    fmt.Println(s.IsLower())  // Should print true


    s = "I'M YELLING AT YOU!"

    fmt.Println(s.IsUpper())  // Should print true

}

注意: s.IsUpper() 和 s.IsLower() 并不真正存在,但如果能找到等价的就很好了。


弑天下
浏览 162回答 4
4回答

炎炎设计

Golang 中检查字符串中所有字符是大写还是小写的简单方法是什么?另外,如何处理字符串有标点符号的情况?请参阅这些示例:package mainimport (    "fmt"    "unicode")func main() {    s := "UPPERCASE"    fmt.Println(s.IsUpper())  // Should print true    s = "lowercase"    fmt.Println(s.IsUpper())  // Should print false    s = "lowercase"    fmt.Println(s.IsLower())  // Should print true    s = "I'M YELLING AT YOU!"    fmt.Println(s.IsUpper())  // Should print true}注意: s.IsUpper() 和 s.IsLower() 并不真正存在,但如果能找到等价的就很好了。

湖上湖

一种解决方案是使用 strings.ToUpper()/ToLower() 并与原始字符串进行比较。这也适用于标点符号的情况。这是解决方案:package mainimport (    "fmt"    "strings")func main() {    s := "UPPERCASE"    fmt.Println(strings.ToUpper(s) == s)    s = "lowercase"    fmt.Println(strings.ToUpper(s) == s)    s = "lowercase"    fmt.Println(strings.ToLower(s) == s)    s = "I'M YELLING AT YOU!"    fmt.Println(strings.ToUpper(s) == s)}

千万里不及你

不需要unicode(仅适用于英文字母):func IsUpper(s string) bool {&nbsp; &nbsp; for _, charNumber := range s {&nbsp; &nbsp; &nbsp; &nbsp; if charNumber > 90 || charNumber < 65 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return true}&nbsp; &nbsp;&nbsp;func IsLower(s string) bool {&nbsp; &nbsp; for _, charNumber := range s {&nbsp; &nbsp; &nbsp; &nbsp; if charNumber > 122 || charNumber < 97 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return true}

HUH函数

A unicode.{IsUpper, Lower}和 B strings.{ToUpper, Lower}都很好对于单个字节组成的数据,A会比B更好如果数据字节不确定,则 B 优于 A:例如中文a1package mainimport (    "strings"    "testing"    "unicode")func IsUpperU(s string) bool {    for _, r := range s {        if !unicode.IsUpper(r) && unicode.IsLetter(r) {            return false        }    }    return true}func IsUpper(s string) bool {    return strings.ToUpper(s) == s}func IsLowerU(s string) bool {    for _, r := range s {        if !unicode.IsLower(r) && unicode.IsLetter(r) {            return false        }    }    return true}func IsLower(s string) bool {    return strings.ToLower(s) == s}func TestIsUpper(t *testing.T) {    for _, d := range []struct {        actual   bool        expected bool    }{        {IsUpperU("中文A1"), false}, // be careful!        {IsUpper("中文A1"), true},        {IsUpper("中文a1"), false},        {IsUpperU("中文a1"), false},    } {        if d.actual != d.expected {            t.Fatal()        }    }}func TestIsLower(t *testing.T) {    for idx, d := range []struct {        actual   bool        expected bool    }{        {IsLowerU("中文a1"), false}, // be careful!        {IsLower("中文a1"), true},        {IsLower("中文A1"), false},        {IsLowerU("中文A1"), false},    } {        if d.actual != d.expected {            t.Fatal(idx)        }    }}go playground
打开App,查看更多内容
随时随地看视频慕课网APP