This is the fourth day of my participation in the August Text Challenge.More challenges in August

String representation

Double quoted string

The escaped characters in the string will be escaped and output after escaping

// Double quoted string
	/* Hello world! * /
str := "Hello \n world!"
fmt.Println(str)
Copy the code

Backquoted string

Escaped characters in a string are not escaped and output as is

// Backquotes string
	/* Hello \n World! * /
str = `Hello \n World! `
fmt.Println(str)
Copy the code

Empty strings are represented by “”, which is the zero value of the string

Length of string

We can use the len() function to get the length of the string bytes

Note that this function takes the sum of all character lengths in the string, not the number of characters

	/* Pure letters, to get the length of the output: 15 */
	str = `Hello \n World! `
	fmt.Println(len(str))

	/* Chinese letters, to get the length of the output: 8 */
	str = "You Go"
	fmt.Println(len(str))
Copy the code

Call utf8.runecountinString () to get the number of characters in the string, not the number of bytes

/* A Chinese letter in bytes. The output is 8 */
str = "You Go"
fmt.Println(len(str))

/* The character length is 4 */
fmt.Println(utf8.RuneCountInString(str))
Copy the code

Traversal string

// Go for... Range decodes implicit types when working with strings
// Iterate over the string with Chinese characters
str = "You Go"
for i, v := range str {
   fmt.Printf("Index: %d, value: %c \n", i, v)
}

// Iterate over a string of pure letters
str = `Hello World! `
for i, v := range str {
   fmt.Printf("Index: %d, value: %c \n", i, v)
}
Copy the code

Concatenation of strings

Overloaded operators +, +=

	// Direct concatenation of strings
	str = "Hello " + "world!"
	str += " I am a gopher"
	fmt.Println(str)
Copy the code

strings.Join()

// When concatenating multiple strings, add a string to separate them
str = strings.Join([] string{"Hello"."world"}, " and ")
fmt.Println(str)
Copy the code

bytes.Buffer

// bytes.buffer concatenates a string
var buffer bytes.Buffer
buffer.WriteString("Hello")
buffer.WriteString(" world!")
fmt.Println(buffer.String())
Copy the code

strings.Builder

// strings.Builder concatenates strings
var builder strings.Builder
builder.WriteString("I")
builder.WriteString(" am")
builder.WriteString(" a")
builder.WriteString(" gopher!")
fmt.Println(builder.String())
Copy the code

The first two methods of concatenating strings have some memory and performance waste. It is recommended to use the latter two methods for concatenating strings

Common string operations

Determine the prefix

/ / prefix
res := strings.HasPrefix("isOpen"."is")
fmt.Println(res)
Copy the code

Determine the suffix

/ / the suffix
res = strings.HasSuffix("finished"."ed")
fmt.Println(res)
Copy the code

Split string

res2 := strings.Split("Hi! Shaosiming! You are nice!"."!")
for _, v := range res2 {
   fmt.Println(v)
}
Copy the code

Returns the substring index

	// The position first appeared from the left
	index := strings.Index("Hi shaosiming"."i")
	fmt.Println(index)

	// First appearing position from the right
	index = strings.LastIndex("Hi shaosiming"."i")
	fmt.Println(index)
Copy the code

String substitution

The first argument is the string to operate on

The second argument is the string to be replaced

The third argument is the string to be replaced with

The fourth parameter is the number of substitutions. -1 represents all substitutions

str = strings.Replace("Hi! Shaosiming! You are nice!"."!".",".- 1)
fmt.Println(str)
Copy the code

String to uppercase String to lowercase

/ / caps
fmt.Println(strings.ToUpper("Hi Shaosiming"))

/ / lowercase
fmt.Println(strings.ToLower("Hi Shaosiming"))
Copy the code

The number of occurrences of characters in a string

fmt.Println(strings.Count("hi shaosiming"."i"))
Copy the code

The inclusion relationship of a string

fmt.Println(strings.Contains("hi shaosiming"."ming"))
Copy the code