This paper introduces the Go standard library Strings commonly exported functions, structures and methods.

import strings
Copy the code

The Strings package implements simple functions for manipulating strings, including the strings export function and the Reader and Replacer constructs.

1. Strings is used to export functions

Determine the relationship between a string and a substring

  • func EqualFold(s, t string) bool// Check two utF-8 encoding strings, case insensitive
  • func HasPrefix(s, prefix string) bool// Determine whether s has a prefix string prefix
  • func Contains(s, substr string) bool// Determine if the string s contains substr
  • func ContainsAny(s, chars string) bool// Determine if the string s contains any characters from the string chars
  • func Count(s, sep string) int// Returns several non-repeating sep substrings in the string s

Gets the position of the string neutron string

  • func Index(s, sep string) int// The position at which the substring sep first appears in the string s, returns -1 if it does not exist
  • func IndexByte(s string, c byte) int// The position where the character c first appears in s, if not, returns –
  • func IndexAny(s, chars string) int// The first occurrence of any UTF-8 code value in s in the string chars, or -1 if none exists or chars is an empty string
  • func IndexFunc(s string, f func(rune) bool) intIf f(r)==true, return -1. If f(r)= true, return -1
  • func LastIndex(s, sep string) int// The last position of the substring sep in string s, if not, -1 is returned

Character handling in a string

  • func Title(s string) string// Returns a copy of the string in s with the first letter of each word in the title format
  • func ToLower(s string) string// Returns a copy with all letters converted to the corresponding lowercase version
  • func ToUpper(s string) string// Returns a copy that converts all letters to the corresponding uppercase version
  • func Repeat(s string, count int) string// Return count as a string concatenated by s
  • func Replace(s, old, new string, n int) string// Returns a new string that replaces the first n nonoverlapping old substrings in s with new, or all old substrings if n<0
  • func Map(mapping func(rune) rune, s string) string// Replace each unicode code value r of s with a mapping(r) to return a copy of the string composed of these new code values. If the mapping returns a negative value, the code value is discarded and not replaced

String front-end and back-end processing

  • func Trim(s string, cutset string) string// Returns a string that strips out all utF-8 code values contained in cutSet on both ends of S
  • func TrimSpace(s string) string// Returns a string with all whitespace (specified by Unicode.isspace) removed from the front and back ends of s
  • func TrimFunc(s string, f func(rune) bool) string// Returns a string that strips out all unicode values that satisfy f at the end of s

String splitting and merging

  • func Fields(s string) []string// Returns multiple strings divided by whitespace (one to more contiguous whitespace characters, as determined by Unicode.isspace)
  • func Split(s, sep string) []string// Partitioning by removing sep from s splits to the end and returns a slice of all the generated fragments
  • func Join(a []string, sep string) string// Concatenate a series of strings into a single string, separated by sep

stringsExport function example

$GOPATH/src/github.com/ironxu/go_note/library/strings/strings.go source code is as follows:

Strings package main import (" FMT ""strings") func main() { S, t := "hello go", "hello go" is_equal := strings.EqualFold(s, t) FMT.Println("EqualFold: ", is_equal) // EqualFold: Prefix prefix := "hello" has_prefix := strings.HasPrefix(s, Prefix) FMT.Println(has_prefix) // true // determine if s HasSuffix string suffix suffix := "go" has_suffix := strings.HasSuffix(s, Suffix) fmt.Println(has_suffix) // true // determine if strings Contains substr substr := "lo" con := strings.Contains(s, Println(con) r r := rune(101) ru := 'e' con_run := strings.containsrune (s, R) FMT.Println(con_run, r, ru) // true // the position where the substring sep first appears in string s, Sep := "o" sep_idnex := strings.index (s, sep) fmt.Println(sep_idnex) // 4 // the last position of the substring sep in the strings, Sep_lastindex := strings.LastIndex(s, Sep) fmt.println (sep_lastindex) // 7 // Returns a string in s with the first letter of each word in the title format copy title := strings.title (s) fmt.println (title) // Hello To_title := strings.totitle (s) fmt.println (to_title) // HELLO Go // Returns a copy that converts all letters to the corresponding lower-case version S_lower := strings.tolower (s) fmt.Println(s_lower) // Hello go // return count s string string s_repeat := strings.repeat (s, 3) fmt.Println(s_repeat) // hello goHello goHello go // Returns a new string that replaces the first n non-overlapping old substrings in s with new, if n&lt; S_old, s_new := "go", "world" s_replace := strings.Replace(s, s_old, s_new, -1) FMT.Println(s_replace) // hello world // Return string s, cutset := "# ABC!!" . "#!" s_new = strings.Trim(s, cutset) fmt.Println(s, s_new) // #abc!!! ABC // returns multiple strings separated by whitespace (determined by Unicode.isspace, which can be one to more consecutive whitespace characters). S = "Hello world! go language" s_fields := strings.Fields(s) for k, v := range s_fields { fmt.Println(k, v) } // 0 hello // 1 world! // 2 go // 3 language // S_split := strings.split (s, "") fmt.println (s_split) // [hello world! Go language] // join a series of strings into a single string, Are separated by sep s_join: = strings. Join (string [] {" a ", "b", "c"}, "/") fmt.println (s_join) // a/b/c // Replaces each unicode code value r of s with a mapping(r) and returns a copy of the string made up of these new code values. If the mapping returns a negative value, the code value is discarded and will not be replaced map_func := func(r rune) rune {switch {case r &gt; 'A' &amp; &amp; r &lt; 'Z': return r + 32 case r &gt; 'a' &amp; &amp; r &lt; 'z': return r - 32 } return r } s = "Hello World!" s_map := strings.Map(map_func, s) fmt.Println(s_map) // hELLO wORLD! }Copy the code

2. Reader structure

The Reader type reads data from a string, implementing IO.Reader, IO.Seeker, etc.

  • func NewReader(s string) *Reader// Pass the stringsTo create aReader
  • func (r *Reader) Len() int/ / returnrThe length of the part has not been read
  • func (r *Reader) Read(b []byte) (n int, err error)// Read some data tob, the length of the read depends onbThe capacity of the
  • func (r *Reader) ReadByte() (b byte, err error)/ / fromrRead a byte of data in

$GOPATH/src/github.com/ironxu/go_note/library/strings/reader.go source code is as follows:

Func main() {s := "hello world" // create Reader r := strings.NewReader(s) fmt.Println(r) // &amp; {hello world 0-1} fmt.println (r.thread ()) // 11 get the length of the string fmt.println (r.thread ()) // 11 Get the length of the string fmt.println (r.thread ()) 5 {b, err := r.readByte () // Read 1 byte fmt.println (string(b), err, r.len (), r.size ())) // h &lt; nil&gt; 10 11 // e &lt; nil&gt; 9 11 // l &lt; nil&gt; 8 11 // l &lt; nil&gt; 7 11 // o &lt; nil&gt; 6 11 // &lt; nil&gt; B_s := make([]byte, 5) n, err := r.read (b_s) FMT.Println(string(b_s), n ,err) // world 5 &lt; nil&gt; fmt.Println(r.Size()) // 11 fmt.Println(r.Len()) // 0 }Copy the code

3. Replacer structure

The Replacer type implements string substitution

  • func NewReplacer(oldnew ... string) *Replacer// Create a *Replacer with the supplied groups of old and new strings
  • func (r *Replacer) Replace(s string) string/ / returnsAll the copies that have been replaced
  • func (r *Replacer) WriteString(w io.Writer, s string) (n int, err error)// Write the replacement copy of s to w

$GOPATH/src/github.com/ironxu/go_note/library/strings/replace.go source code is as follows:

// Go library strings.Replacer package main import (" FMT ""strings" "OS ") func main() {s := "go Language" r := strings.NewReplacer("&lt;" , "&lt;" , "&gt;" , "&gt;" ) fmt.Println(r.Replace(s)) r.WriteString(os.Stdout, s) }Copy the code

reference

  • PKG/strings in Chinese
  • pkg/strings