1, the string type
String is a value type, and the value can not be changed, that will create a text can not modify the content of this text again, fixed-length string is a byte array.
1) the definition of the string
Double quotes may be used '' to define the string of characters may be used to implement the escape line breaks, indents effect.
General comparison operator (==,! =, <, <=,> =,>) In the memory byte by byte comparison is achieved by comparing the character string, so that the result of the comparison string NATURAL coding sequence. Length of the string of bytes occupied by the function may be obtained by len ().
Contents of the string (bytes pure) may be obtained by standard indexing method [Write index] in square brackets, from index 0 starts counting.
2) string concatenation operator "+"
Two strings s1 and s2 by s: = s1 + s2 spliced together. S2 s1 appended to the tail of the string, and generates a new s. You can also use the "+ =" splice to strings.
Example:
package main import "fmt" func main() { var s1 = "hello" + ", world" var s2 = "hello" + ", world" var s3 = "hello" s3 += ", world" fmt.Println(s1) fmt.Println(s2) fmt.Println(s3) }
3) Based on the string in UTF-8
Go internal language string using UTF-8 encoding implemented, can easily access to each rune by UTF-8 character type. Of course, Go language also supports access by character according to the traditional way of ASCII code.
4) defines a plurality of rows String
In the Go language, use double quotation marks to write the way the string is a common expression string, known as string literals (string literal), this is not a literal double quotes interbank, if you want to be embedded in the source code when a multi-line strings, you must use `
anti-quotation marks.
In this way, inter-exchange lines will be used as anti-quotation marks in the string line breaks, but all escaped characters are invalid and will be output as text.
Example:
package main import "fmt" func main() { var str = `Hello \ n World \ n` fmt.Println(str) }
2, calculating the string length
len () Returns the value of the function is int, ASCII characters or the number of bytes of the length of the string.
If you wish to be calculated according to the number of characters on the habit, you need to use the Go language in UTF-8 RuneCountInString () function, statistical Uncode number of characters packages.
Example:
package main import ( "fmt" "unicode/utf8" ) func main() { var s1 = "abc" var s2 = "Hello a" fmt.Printf("s1: %v\n", len(s1)) // 3 fmt.Printf("s2: %v\n", len(s2)) // 7 fmt.Printf("s1: %v\n", utf8.RuneCountInString(s1)) // 3 fmt.Printf("s2: %v\n", utf8.RuneCountInString(s2)) // 3 }
3, traversal strings
ASCII string directly traverse a subscript.
Traversed with Unicode strings for range.
Example:
package main import "fmt" func main() { var str = "hello, world" for i := 0; i < len(str) ; i++ { fmt.Printf("%c, %d\n", str[i], str[i]) } fmt.Println("-------------------------") for _, s := range str{ fmt.Printf("%c, %d\n", s, s) } }
4, string interception
strings.Index: Forward substring search.
strings.LastIndex: Reverse search substring.
Start of the search may be offset produced by slicing.
Example:
package main import ( "fmt" "strings" ) func main() { var str = "hello, world, new" pos := strings.Index(str, ",") lastPos := strings.LastIndex(str, ",") fmt.Printf("pos = %d, lastPos=%d\n", pos, lastPos) // pos = 6, lastPos=13 }
5, modify the string
Go language strings are immutable.
Modifying the string, the string can be converted to [] byte, modification.
[] and byte string can be cast by the system conversion.
Example:
package main import "fmt" func main() { var str = "hello, world" strBytes := []byte(str) for i := 5; i <= 6 ; i++ { strBytes[i] = '-' } fmt.Println(string(strBytes)) // hello--world }
6, string concatenation
In addition to the plus connection string, Go is also a similar StringBuilder efficient mechanism to connect strings.
Example:
package main import ( "bytes" "fmt" ) func main() { var stringBuilder bytes.Buffer // declaration byte buffer // string written into the buffer stringBuilder.WriteString("hello,") stringBuilder.WriteString("世界") // string output buffer fmt.Println(stringBuilder.String()) }
7, formatted output
fmt.Sprintf (formatting style, the argument list ...)
Format pattern: a string, formatted in% at the beginning of the verb.
Parameter List: multiple parameters separated by a comma, the number must correspond to the number of formatting styles, otherwise it will error runtime.
Example:
package main import "fmt" func main() { name := "hello" str := fmt.Sprintf("%s, world", name) fmt.Println(str) }