Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use string manipulation functions commonly used in Golang strings package

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "Golang strings package commonly used string manipulation functions how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Golang strings package commonly used string manipulation functions how to use" it!

Func Contains

Func Contains (s, substr string) bool

Function: to determine whether substr is a substring of s, for example:

Package main import ("fmt"strings") func main () {fmtPrintln (stringsContains ("Linux", "in")) fmtPrintln (stringsContains ("Linux", "Unix")) fmtPrintln (stringsContains ("Linux", ")) fmtPrintln (stringsContains (", "))}

Output:

True

False

True

True

Func HasPrefix

Func HasPrefix (s, prefix string) bool

Function: whether the string s begins with prefix, for example:

Package main import ("fmt"strings") func main () {fmtPrintln (stringsHasPrefix ("Linux", "Lin")) fmtPrintln (stringsHasPrefix ("Linux", "in")) fmtPrintln (stringsHasPrefix ("Linux", "))}

Output:

True

False

True

Func HasSuffix

Func HasSuffix (s, suffix string) bool

Purpose: to determine whether the string s ends with suffix, for example:

Package main import ("fmt"strings") func main () {fmtPrintln (stringsHasSuffix ("Linux", "nux")) fmtPrintln (stringsHasSuffix ("Linux", "ix")) fmtPrintln (stringsHasSuffix ("Linux", "))}

Output:

True

False

True

Func Replace

Func Replace (s, old, new string, n int) string

Function: return the first n non-repeating old substrings in s to replace the new new substrings. If n < 0, replace all old substrings, for example:

Package main import ("fmt"strings") func main () {fmtPrintln (stringsReplace ("Linux is very very very good!", "very", "much", 2)) fmtPrintln (stringsReplace ("Linux is very very very good!", "very", "much",-1))}

Output:

Linux is much much very good!

Linux is much much much good!

Func Split

Func Split (s, sep string) [] string

Function: returns the string slice that divides the string s into sep substrings. When sep is an empty string, s is divided into string slices of each unicode code value. For example:

Package main import ("fmt"strings") func main () {fmtPrintln (stringsSplit ("Linux, Unix, Windows, Android", ",")) fmtPrintln (stringsSplit ("Linux is very very very good!", "))}

Output: returns an array of strings.

[Linux Unix Windows Android]

[Linux is very very very good!]

Func ToLower

Func ToLower (s string) string

Function: returns a lowercase copy of the letters in the string s, for example:

Package main import ("fmt"strings") func main () {fmtPrintln (stringsToLower ("Linux, Unix, Windows, Android") fmtPrintln (stringsToLower ("Linux is very very very good!"))}

Output:

Linux, unix, windows, android

Linux is very very very good!

Func ToUpper

Func ToUpper (s string) string

Function: returns a copy of the uppercase letters in the string s, for example:

Package main import ("fmt"strings") func main () {fmtPrintln (stringsToUpper ("Linux, Unix, Windows, Android") fmtPrintln (stringsToUpper ("Linux is very very very good!"))}

Output:

LINUX, UNIX, WINDOWS, ANDROID

LINUX IS VERY VERY VERY GOOD!

Func Repeat

Func Repeat (s string, count int) string

Function: returns a string concatenated by count strings. If count is negative or the result of (len * s * count) overflows, an panic exception is reported. For example:

Package main import ("fmt"strings") func main () {fmtPrintln (stringsRepeat ("Linux", 6)) fmtPrintln (stringsRepeat ("Linux", 0)) fmtPrintln (stringsRepeat ("Linux",-1))}

Output:

LinuxLinuxLinuxLinuxLinuxLinux

Panic: strings: negative Repeat count

Goroutine 1 [running]:

StringsRepeat (0x4bcf3d, 0x5, 0xffffffffffffffff, 0x1, 0x1)

/ usr/local/go/src/strings/stringsgo:529 + 0x5e5

Mainmain ()

/ root/goProject/src/test/maingo:11 + 0x167

Func Count

Func Count (s, substr string) int

Function: returns the number of non-overlapping substrings substr contained in the string s. If substr is an empty string, return the number of Unicode code points in 1 + s (Unicode code point: it can be simply understood that a symbol is a code point), for example:

Package main import ("fmt"strings") func main () {fmtPrintln (stringsCount ("Golang Golang Golang", "Go")) fmtPrintln (stringsCount ("Golang", ")) fmtPrintln (stringsCount (" Golang language ","))}

Output:

three

seven

nine

Func Index

Func Index (s, substr string) int

Function: returns the index of the first substring substr contained in the string s, and returns-1 if it does not exist, for example:

Package main import ("fmt"strings") func main () {fmtPrintln (stringsIndex ("Golang", "lang")) fmtPrintln (stringsIndex ("Golang", "Linux"))}

Output:

two

-1

Func Join

Func Join (elems [] string, sep string) string

Purpose: use sep as a delimiter to concatenate all characters in elems, for example:

Package main import ("fmt"strings") func main () {elems: = [] string {"I", "like", "golang", "!"} fmtPrintln (stringsJoin (elems, ") elems = [] string {" 123,456,789 "} fmtPrintln (stringsJoin (elems,"-"))}

Output:

[root@localhost gotest] # go run maingo

I like golang!

123-456-789

[root@localhost gotest] #

Thank you for reading, the above is the "Golang strings package commonly used string manipulation functions how to use" the content, after the study of this article, I believe you on the Golang strings package commonly used string manipulation functions how to use this problem have a deeper understanding, the specific use of the situation also need to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report