当前位置:网站首页>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
边栏推荐
- Using mock data in vite projects -vite plugin mock
- OVS port traffic statistics practice
- Three ways to get class
- HTTP cache
- Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027
- Performance test -- 14 detailed explanation of performance test report and precautions
- CSDN browser assistant for online translation, calculation, learning and removal of all advertisements
- Delta oscillation in EEG
- Hypervisor Necromancy; Recover kernel protector (1)
- Pnas: amygdala individual specific functional connectivity: Fundamentals of precision psychiatry
猜你喜欢

How to design API return codes (error codes)?

Evolution history of mobile communication

Xgboost Guide

JS rotation chart (Netease cloud rotation chart)

Digital circuit logic design

Deep learning environment configuration (III) pytorch GPU under Anaconda

Anaconda creates a new environment encounter pit

Information theory and coding

Data analysis method - user group analysis

what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!
随机推荐
Analog Electronic Technology
Using mock data in vite projects -vite plugin mock
Mongodb aggregate query implements multi table associated query, type conversion, and returns specified parameters.
Practice and exploration of vivo live broadcast application technology
Error in OpenCV image operation: error: (-215:assertion failed)_ src. empty() in function ‘cv::cvtColor‘
How to design API return codes (error codes)?
Vs code remote SSH configuration
"Return index" of live broadcast E-commerce
Detailed explanation of online reputation management
Application and challenge of ten billion level map data in Kwai security intelligence
Microservice Optimization: internal communication of microservices using grpc
C language series - Section 4 - arrays
Evolution history of mobile communication
Function recursion and iteration
Troubleshooting and optimization of easynvr version 5.0 Video Square snapshot not displayed
Summary of easy-to-use MySQL interview questions (Part 1)
Digital circuit logic design
How to batch make decreasing serial number barcode
Extract NTDs with volume shadow copy service dit
There is no WM data in the primary commodity master data store view of SAP retail