Go language string basics

 

Table of contents

String Basics

Merge and split strings

Split

merge

Determine whether to include

strings.Contains()

find occurrences of substring

strings.Index()

strings.LastIndex()


String Basics

1. A string is a sequence of Unicode characters, and each Unicode character occupies one or more bytes of storage space.

2. Strings are enclosed in double quotes ("") or backticks (``) in the Go language, for example: "Hello, World!" and Hello, World!.

3. The string can access each character through the subscript, the subscript starts from 0, for example: s[0] represents the first character in the string s.

4. Strings can be concatenated using the plus sign (+), for example: "Hello" + "World" will get "HelloWorld".

5. You can use the len() function to get the length of the string, for example: len("Hello, World!") will get 13.

6. The string can use the for loop to traverse each character, for example:

s := "Hello, World!"
for i := 0; i < len(s); i++ {
    fmt.Printf("%c ", s[i])
}

1. Strings are immutable and cannot be modified once created. If you want to modify a string, you need to convert it to a byte array or rune array.

2. Strings can be converted using the strconv package, such as converting strings to integers or floating point numbers.

3. Strings can use the functions provided by the strings package to perform various operations, such as:

(1) strings.Contains(s, substr) Determine whether the string s contains the substring substr

(2) strings.Index(s, substr) Returns the position where the substring substr first appears in the string s, or -1 if it does not exist

(3) strings.LastIndex(s, substr) Returns the last occurrence of the substring substr in the string s, or -1 if it does not exist

(4) strings.Replace(s, old, new, n) Replace the first n old substrings in the string s with new substrings, if n is -1, replace all old substrings

(5) strings.Split(s, sep) splits the string s into multiple substrings according to the delimiter sep, and returns a string slice

(6) strings.Trim(s, cutset) removes the cutset characters before and after the string s, and returns a new string

Merge and split strings

Split

You need to import the strings package first. The strings package provides four functions: Split(), SplitN(), SplitAfter(), and SplitAfterN() to process split strings

(1) The Split() function is defined as follows:

func Split(s, sep string) []string

s is the string to be split, sep is the separator, and string is the type of the separator

(2) The SplitN() function is defined as follows:

func SplitN(s, sep string, n int) []string

s is the string to be split, sep is the separator, string is the type of the separator, n is the number of slices to control the split

(3) The SplitAfter() function is defined as follows:

func SplitAfter(s, sep string)

s is the string to be split, sep is the separator, and string is the type of the separator

(4) The SplitAfterN() function is defined as follows:

func SplitAfterN(s, sep string, n int) []string

s is the string to be split, sep is the separator, string is the type of the separator, n is the number of slices to control the split

These four functions split the incoming string parameter s through the sep parameter, and the return type is []string, if the sep parameter is empty, it will be divided into a UTF-8 character.

For example

Use strings.Split() to split strings, you need to introduce the strings package

str := "123-456-789"

arr := strings.Split(str, "-")

fmt.Println(arr)//分割结果为: [123 456 789]

merge

The Go language strings package provides a Join() function to combine strings. The format of the source code is as follows:

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

The first parameter is an array of strings accepted, the latter parameter is the sep separator

For example

Use strings.Join() to concatenate slices into strings

str := "123-456-789"

arr := strings.Split(str, "-")

str2 := strings.Join(arr, " ")

fmt.Println(str2)//拼接后的结果为: 123 456 789

Determine whether to include

strings.Contains()

Use strings.Contains() to determine whether it is included. The definition of this function is as follows:

func Contains(s, substr string) bool

s is the accepted string, substr is the string that needs to be checked to determine whether it exists

For example

str1 := "你好GoLang"

str2 := "GoLang"

flag := strings.Contains(str1, str2)

fmt.Println(flag)//输出的值为:true

find occurrences of substring

strings.Index()

Use strings.Index() to find the position where the substring appears forward. The format of the function is as follows:

func Index(s, substr string) int

s is the string to be searched, substr is the substring to be searched, returns the first occurrence of the substring substr in the string s, and returns -1 if it does not exist

For example

str1 := "I love you"

str2 := "l"

num := strings.Index(str1, str2)

fmt.Println(num)//输出结果为: 2

strings.LastIndex()

Use strings.Index() to find the position where the substring appears in reverse. The format of the function is as follows:

func LastIndex(s, substr string) int

s is the string to be searched, substr is the substring to be searched, and the substring that meets the requirements is searched from the back to the front, but it should be noted that the returned position is the position from the front to the back of the substring, if it does not exist, return -1

For example

str1 := "I love you"

str2 := "o"

num := strings.LastIndex(str1, str2)

fmt.Println(num)//输出结果为: 8

Guess you like

Origin blog.csdn.net/weixin_71646897/article/details/130571433