当前位置:网站首页>Golang string comparison
Golang string comparison
2022-06-23 02:36:00 【Wangxiaoming_ HIT】
golang String comparison
String comparison , You can use it directly == Compare , It can also be used strings.Compare Compare
go There are three ways to compare strings in :
==Comparestrings.CompareComparestrings.EquslFoldCompare
#### Code example
```go
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"))The above code execution results are as follows :
true false -1 0 true
Compare and EqualFold difference
EqualFoldIs the more UTF-8 Whether the codes are equal in lowercase , Case insensitive
// EqualFold reports whether s and t, interpreted as UTF-8 strings, // are equal under Unicode case-folding. func EqualFold(s, t string) bool
- It should be noted that
CompareFunctions are case sensitive ,==Faster execution
/ 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
Ignore case comparison
Sometimes you have to ignore the case comparison , have access to strings.EqualFold Compare strings for equality
The source code to achieve
// EqualFold reports whether s and t, interpreted as UTF-8 strings,
// are equal under Unicode case-folding, which is a more general
// form of case-insensitivity.
func EqualFold(s, t string) bool {
for s != "" && t != "" {
// Extract first rune from each string.
var sr, tr rune
if s[0] < utf8.RuneSelf {
sr, s = rune(s[0]), s[1:]
} else {
r, size := utf8.DecodeRuneInString(s)
sr, s = r, s[size:]
}
if t[0] < utf8.RuneSelf {
tr, t = rune(t[0]), t[1:]
} else {
r, size := utf8.DecodeRuneInString(t)
tr, t = r, t[size:]
}
// If they match, keep going; if not, return false.
// Easy case.
if tr == sr {
continue
}
// Make sr < tr to simplify what follows.
if tr < sr {
tr, sr = sr, tr
}
// Fast check for ASCII.
if tr < utf8.RuneSelf {
// ASCII only, sr/tr must be upper/lower case
if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' {
continue
}
return false
}
// General case. SimpleFold(x) returns the next equivalent rune > x
// or wraps around to smaller values.
r := unicode.SimpleFold(sr)
for r != sr && r < tr {
r = unicode.SimpleFold(r)
}
if r == tr {
continue
}
return false
}
// One string is empty. Are both?
return s == t
} Through the source code, you can see if 'A' <= sr && sr <= 'Z' && tr == sr+'a'-'A' You can see the case insensitive implementation .
Look at a complete test code :
// Golang program to illustrate the
// strings.EqualFold() Function
package main
// importing fmt and strings
import (
"fmt"
"strings"
)
// calling main method
func main() {
// case insensitive comparing and returns true.
fmt.Println(strings.EqualFold("Geeks", "Geeks"))
// case insensitive comparing and returns true.
fmt.Println(strings.EqualFold("computerscience", "computerscience"))
}Execution structure
true true
Reference material
- https://www.geeksforgeeks.org/strings-equalfold-function-in-golang-with-examples/
- https://cloud.tencent.com/developer/article/1651885
边栏推荐
- February 6, 2022: Arithmetic Sequence Division II - subsequence. Give you an integer array n
- How to use fortress on mobile devices
- Wechat applet camera compressed image is Base64
- What is ISBN code and how to make it
- JS to realize the rotation chart (riding light). Pictures can be switched left and right. Moving the mouse will stop the rotation
- Application and challenge of ten billion level map data in Kwai security intelligence
- Deep analysis of time complexity
- Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027
- How to store, manage and view family photos in an orderly manner?
- 6. template for integer and real number dichotomy
猜你喜欢

How to design API return codes (error codes)?

Deep learning environment configuration (III) pytorch GPU under Anaconda

Spark broadcast variables and accumulators (cases attached)

Deep learning environment configuration (I) installation of CUDA and cudnn

Reptile lesson 1

How to make word notes beautiful
What is sitelock? What is the function?

5g access network and base station evolution
![Buuctf misc-[bjdctf2020] Nani](/img/4e/ac6bf2f64cb68136581814da73db66.jpg)
Buuctf misc-[bjdctf2020] Nani

Small knowledge points of asset
随机推荐
Xgboost principle
Wechat applet camera compressed image is Base64
1.3-1.4 web page data capture
Easygbs adds websocket message push, which can quickly locate video playback faults
Applet control version update best practices
You must know the type and method of urllib
Aikuai multi dialing + load balancing overlay bandwidth
Summary of easy-to-use MySQL interview questions (Part 1)
"Return index" of live broadcast E-commerce
Use of apicloud AVM framework list component list view and flex layout tutorial
Li Mu's notes on machine learning -1.2
6. template for integer and real number dichotomy
JS rotation chart (Netease cloud rotation chart)
Get the structure of the class through reflection, little chestnut
await is only valid in async function
what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!
Vulnhub DC-5
C language series - Section 4 - arrays
Analog Electronic Technology
Anaconda creates a new environment encounter pit