Home > Backend Development > Golang > golang string method

golang string method

WBOY
Release: 2023-05-22 14:01:40
Original
847 people have browsed it

Golang is an efficient, simple and reliable programming language. It provides many convenient string processing methods, allowing programmers to easily process strings. In this article, we will focus on Golang’s string methods.

1. String definition

The way to define a string in Golang is very simple. You can use single quotes or double quotes to represent a string. For example:

str1 := "Hello, world!" 
str2 := 'G'
Copy after login

2. String length

Use the len() string method in Go to get the length of the string. This method returns how much the string contains. character. For example:

str := "golang string methods"
length := len(str) // length等于22
Copy after login

3. String splicing

In Golang, use the plus sign ( ) to splice two strings. For example:

str1 := "Hello"
str2 := "World"
mergedStr := str1 + " " + str2 //mergedStr的值是"Hello World"
Copy after login

A more convenient way is to use the fmt.Sprintf() method, which supports placeholders. For example:

age := 30
str := fmt.Sprintf("My age is %d", age) // str的值是"My age is 30"
Copy after login

4. Substring search

Go provides some methods to check whether a string contains a substring and get the location of the substring. We can use the strings.HasPrefix(), strings.HasSuffix() and strings.Contains() methods to accomplish these operations. For example:

str := "golang string methods"
hasPrefix := strings.HasPrefix(str, "golang") //返回true
hasSuffix := strings.HasSuffix(str, "methods") //返回true
contains := strings.Contains(str, "string") //返回true
Copy after login

If we need to find the position where a substring appears, we can use the strings.Index() or strings.LastIndex() method. For example:

str := "golang string methods"
index := strings.Index(str, "string") //index等于7,因为"string"的起始位置是7
lastIndex := strings.LastIndex(str, "th") //lastIndex等于14,因为"th"最后出现的位置是14
Copy after login

We can also use strings.IndexAny() and strings.LastIndexAny() to find the position of any character in the string. For example:

str := "golang string methods"
index := strings.IndexAny(str, "aeiou") //index等于1,因为字符"a"在位置1
lastIndex := strings.LastIndexAny(str, "aeiou") //lastIndex等于16,因为字符"o"在位置16
Copy after login

5. String splitting

Splitting a string is one of the very common operations when processing strings. We can use the strings.Split() method to split a string according to a certain character or substring. For example:

str := "golang string methods"
words := strings.Split(str, " ") //words等于["golang", "string", "methods"]
Copy after login

6. String replacement

If you need to replace in a string, you can use the strings.Replace() method. This method receives four parameters: the source string, the substring to be replaced, the new substring, and the number of replacements. For example:

str := "golang string methods"
newStr := strings.Replace(str, "methods", "functions", 1) // newStr的值是"golang string functions"
Copy after login

7. String case conversion

Go language provides strings.ToLower() and strings.ToUpper() methods to perform string case conversion. For example:

str := "Golang is a great language"
lower := strings.ToLower(str) // lower的值是"golang is a great language"
upper := strings.ToUpper(str) // upper的值是"GOLANG IS A GREAT LANGUAGE"
Copy after login

8. String trimming

When processing strings, we sometimes need to delete the spaces in the string or remove the leading space of the characters. The Go language provides strings.TrimSpace(), strings.Trim() and strings.TrimLeft() methods to complete these operations. For example:

str := "  golang string methods  "
trimSpace := strings.TrimSpace(str) //trimSpace的值是"golang string methods"
trim := strings.Trim(str, " ") //trim的值是"golang string methods"
trimLeft := strings.TrimLeft(str, " ") //trimLeft的值是"golang string methods  "
Copy after login

The above are some commonly used string methods in Golang. These methods are very useful and can improve the efficiency and convenience of programmers in processing strings. Hope this article can be helpful to readers.

The above is the detailed content of golang string method. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template