zoukankan      html  css  js  c++  java
  • [转] golang 字符串比较是否相等

    1 前言

    strings.EqualFold不区分大小写,"==" 区分且直观。

    2 代码

    golang字符串比较的三种常见方法
    
    fmt.Println("go"=="go")
    fmt.Println("GO"=="go")
    
    fmt.Println(strings.Compare("GO","go"))
    fmt.Println(strings.Compare("go","go"))
    
    fmt.Println(strings.EqualFold("GO","go"))
    输出
    
    true
    false
    -1
    0
    true
    1,自建方法“==”,区分大小写,最简单的方法
    
    2,Compare函数,区分大小写,比自建方法“==”的速度要快,下面是注释
    / Compare is included only for symmetry with package bytes.
    // It is usually clearer and always faster to use the built-in
    // string comparison operators ==, <, >, and so on.
    func Compare(a, b string) int
    
    3,比较UTF-8编码在小写的条件下是否相等,不区分大小写,下面是注释
    // EqualFold reports whether s and t, interpreted as UTF-8 strings,
    // are equal under Unicode case-folding.
    func EqualFold(s, t string) bool
    

    3 参考

    转摘:https://studygolang.com/articles/14163  

  • 相关阅读:
    人生中最重要的三位老师
    自我介绍
    秋季学习总结
    第五周学习总结
    第四周总结
    第三周基础作业
    判断上三角矩阵
    第二周作业及编程总结
    求最大值及其下标
    查找整数
  • 原文地址:https://www.cnblogs.com/fanbi/p/11956195.html
Copyright © 2011-2022 走看看