Package strings implements simple functions to manipulate UTF-8 encoded strings.
For information about UTF-8 strings in Go, see https://blog.golang.org/strings.
builder.go compare.go reader.go replace.go search.go strings.go
func Compare(a, b string) int
Compare returns an integer comparing two strings lexicographically. The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
Compare is included only for symmetry with package bytes. It is usually clearer and always faster to use the built-in string comparison operators ==, <, >, and so on.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.Compare("a", "b")) fmt.Println(strings.Compare("a", "a")) fmt.Println(strings.Compare("b", "a")) }
func Contains(s, substr string) bool
Contains reports whether substr is within s.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.Contains("seafood", "foo")) fmt.Println(strings.Contains("seafood", "bar")) fmt.Println(strings.Contains("seafood", "")) fmt.Println(strings.Contains("", "")) }
func ContainsAny(s, chars string) bool
ContainsAny reports whether any Unicode code points in chars are within s.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.ContainsAny("team", "i")) fmt.Println(strings.ContainsAny("fail", "ui")) fmt.Println(strings.ContainsAny("ure", "ui")) fmt.Println(strings.ContainsAny("failure", "ui")) fmt.Println(strings.ContainsAny("foo", "")) fmt.Println(strings.ContainsAny("", "")) }
func ContainsRune(s string, r rune) bool
ContainsRune reports whether the Unicode code point r is within s.
package main import ( "fmt" "strings" ) func main() { // Finds whether a string contains a particular Unicode code point. // The code point for the lowercase letter "a", for example, is 97. fmt.Println(strings.ContainsRune("aardvark", 97)) fmt.Println(strings.ContainsRune("timeout", 97)) }
func Count(s, substr string) int
Count counts the number of non-overlapping instances of substr in s. If substr is an empty string, Count returns 1 + the number of Unicode code points in s.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.Count("cheese", "e")) fmt.Println(strings.Count("five", "")) // before & after each rune }
func EqualFold(s, t string) bool
EqualFold reports whether s and t, interpreted as UTF-8 strings, are equal under Unicode case-folding, which is a more general form of case-insensitivity.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.EqualFold("Go", "go")) }
func Fields(s string) []string
Fields splits the string s around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace, returning a slice of substrings of s or an empty slice if s contains only white space.
package main import ( "fmt" "strings" ) func main() { fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz ")) }
func FieldsFunc(s string, f func(rune) bool) []string
FieldsFunc splits the string s at each run of Unicode code points c satisfying f(c) and returns an array of slices of s. If all code points in s satisfy f(c) or the string is empty, an empty slice is returned.
FieldsFunc makes no guarantees about the order in which it calls f(c) and assumes that f always returns the same value for a given c.
package main import ( "fmt" "strings" "unicode" ) func main() { f := func(c rune) bool { return !unicode.IsLetter(c) && !unicode.IsNumber(c) } fmt.Printf("Fields are: %q", strings.FieldsFunc(" foo1;bar2,baz3...", f)) }
func HasPrefix(s, prefix string) bool
HasPrefix tests whether the string s begins with prefix.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.HasPrefix("Gopher", "Go")) fmt.Println(strings.HasPrefix("Gopher", "C")) fmt.Println(strings.HasPrefix("Gopher", "")) }
func HasSuffix(s, suffix string) bool
HasSuffix tests whether the string s ends with suffix.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.HasSuffix("Amigo", "go")) fmt.Println(strings.HasSuffix("Amigo", "O")) fmt.Println(strings.HasSuffix("Amigo", "Ami")) fmt.Println(strings.HasSuffix("Amigo", "")) }
func Index(s, substr string) int
Index returns the index of the first instance of substr in s, or -1 if substr is not present in s.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.Index("chicken", "ken")) fmt.Println(strings.Index("chicken", "dmr")) }
func IndexAny(s, chars string) int
IndexAny returns the index of the first instance of any Unicode code point from chars in s, or -1 if no Unicode code point from chars is present in s.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.IndexAny("chicken", "aeiouy")) fmt.Println(strings.IndexAny("crwth", "aeiouy")) }
func IndexByte(s string, c byte) int
IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.IndexByte("golang", 'g')) fmt.Println(strings.IndexByte("gophers", 'h')) fmt.Println(strings.IndexByte("golang", 'x')) }
func IndexFunc(s string, f func(rune) bool) int
IndexFunc returns the index into s of the first Unicode code point satisfying f(c), or -1 if none do.
package main import ( "fmt" "strings" "unicode" ) func main() { f := func(c rune) bool { return unicode.Is(unicode.Han, c) } fmt.Println(strings.IndexFunc("Hello, 世界", f)) fmt.Println(strings.IndexFunc("Hello, world", f)) }
func IndexRune(s string, r rune) int
IndexRune returns the index of the first instance of the Unicode code point r, or -1 if rune is not present in s. If r is utf8.RuneError, it returns the first instance of any invalid UTF-8 byte sequence.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.IndexRune("chicken", 'k')) fmt.Println(strings.IndexRune("chicken", 'd')) }
func Join(elems []string, sep string) string
Join concatenates the elements of its first argument to create a single string. The separator string sep is placed between elements in the resulting string.
package main import ( "fmt" "strings" ) func main() { s := []string{"foo", "bar", "baz"} fmt.Println(strings.Join(s, ", ")) }
func LastIndex(s, substr string) int
LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.Index("go gopher", "go")) fmt.Println(strings.LastIndex("go gopher", "go")) fmt.Println(strings.LastIndex("go gopher", "rodent")) }
func LastIndexAny(s, chars string) int
LastIndexAny returns the index of the last instance of any Unicode code point from chars in s, or -1 if no Unicode code point from chars is present in s.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.LastIndexAny("go gopher", "go")) fmt.Println(strings.LastIndexAny("go gopher", "rodent")) fmt.Println(strings.LastIndexAny("go gopher", "fail")) }
func LastIndexByte(s string, c byte) int
LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.LastIndexByte("Hello, world", 'l')) fmt.Println(strings.LastIndexByte("Hello, world", 'o')) fmt.Println(strings.LastIndexByte("Hello, world", 'x')) }
func LastIndexFunc(s string, f func(rune) bool) int
LastIndexFunc returns the index into s of the last Unicode code point satisfying f(c), or -1 if none do.
package main import ( "fmt" "strings" "unicode" ) func main() { fmt.Println(strings.LastIndexFunc("go 123", unicode.IsNumber)) fmt.Println(strings.LastIndexFunc("123 go", unicode.IsNumber)) fmt.Println(strings.LastIndexFunc("go", unicode.IsNumber)) }
func Map(mapping func(rune) rune, s string) string
Map returns a copy of the string s with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the string with no replacement.
package main import ( "fmt" "strings" ) func main() { rot13 := func(r rune) rune { switch { case r >= 'A' && r <= 'Z': return 'A' + (r-'A'+13)%26 case r >= 'a' && r <= 'z': return 'a' + (r-'a'+13)%26 } return r } fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher...")) }
func Repeat(s string, count int) string
Repeat returns a new string consisting of count copies of the string s.
It panics if count is negative or if the result of (len(s) * count) overflows.
package main import ( "fmt" "strings" ) func main() { fmt.Println("ba" + strings.Repeat("na", 2)) }
func Replace(s, old, new string, n int) string
Replace returns a copy of the string s with the first n non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string. If n < 0, there is no limit on the number of replacements.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2)) fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1)) }
func ReplaceAll(s, old, new string) string
ReplaceAll returns a copy of the string s with all non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.ReplaceAll("oink oink oink", "oink", "moo")) }
func Split(s, sep string) []string
Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators.
If s does not contain sep and sep is not empty, Split returns a slice of length 1 whose only element is s.
If sep is empty, Split splits after each UTF-8 sequence. If both s and sep are empty, Split returns an empty slice.
It is equivalent to SplitN with a count of -1.
package main import ( "fmt" "strings" ) func main() { fmt.Printf("%q\n", strings.Split("a,b,c", ",")) fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a ")) fmt.Printf("%q\n", strings.Split(" xyz ", "")) fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins")) }
func SplitAfter(s, sep string) []string
SplitAfter slices s into all substrings after each instance of sep and returns a slice of those substrings.
If s does not contain sep and sep is not empty, SplitAfter returns a slice of length 1 whose only element is s.
If sep is empty, SplitAfter splits after each UTF-8 sequence. If both s and sep are empty, SplitAfter returns an empty slice.
It is equivalent to SplitAfterN with a count of -1.
package main import ( "fmt" "strings" ) func main() { fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ",")) }
func SplitAfterN(s, sep string, n int) []string
SplitAfterN slices s into substrings after each instance of sep and returns a slice of those substrings.
The count determines the number of substrings to return:
n > 0: at most n substrings; the last substring will be the unsplit remainder. n == 0: the result is nil (zero substrings) n < 0: all substrings
Edge cases for s and sep (for example, empty strings) are handled as described in the documentation for SplitAfter.
package main import ( "fmt" "strings" ) func main() { fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2)) }
func SplitN(s, sep string, n int) []string
SplitN slices s into substrings separated by sep and returns a slice of the substrings between those separators.
The count determines the number of substrings to return:
n > 0: at most n substrings; the last substring will be the unsplit remainder. n == 0: the result is nil (zero substrings) n < 0: all substrings
Edge cases for s and sep (for example, empty strings) are handled as described in the documentation for Split.
package main import ( "fmt" "strings" ) func main() { fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2)) z := strings.SplitN("a,b,c", ",", 0) fmt.Printf("%q (nil = %v)\n", z, z == nil) }
func Title(s string) string
Title returns a copy of the string s with all Unicode letters that begin words mapped to their Unicode title case.
BUG(rsc): The rule Title uses for word boundaries does not handle Unicode punctuation properly.
package main import ( "fmt" "strings" ) func main() { // Compare this example to the ToTitle example. fmt.Println(strings.Title("her royal highness")) fmt.Println(strings.Title("loud noises")) fmt.Println(strings.Title("хлеб")) }
func ToLower(s string) string
ToLower returns s with all Unicode letters mapped to their lower case.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.ToLower("Gopher")) }
func ToLowerSpecial(c unicode.SpecialCase, s string) string
ToLowerSpecial returns a copy of the string s with all Unicode letters mapped to their lower case using the case mapping specified by c.
package main import ( "fmt" "strings" "unicode" ) func main() { fmt.Println(strings.ToLowerSpecial(unicode.TurkishCase, "Önnek İş")) }
func ToTitle(s string) string
ToTitle returns a copy of the string s with all Unicode letters mapped to their Unicode title case.
package main import ( "fmt" "strings" ) func main() { // Compare this example to the Title example. fmt.Println(strings.ToTitle("her royal highness")) fmt.Println(strings.ToTitle("loud noises")) fmt.Println(strings.ToTitle("хлеб")) }
func ToTitleSpecial(c unicode.SpecialCase, s string) string
ToTitleSpecial returns a copy of the string s with all Unicode letters mapped to their Unicode title case, giving priority to the special casing rules.
package main import ( "fmt" "strings" "unicode" ) func main() { fmt.Println(strings.ToTitleSpecial(unicode.TurkishCase, "dünyanın ilk borsa yapısı Aizonai kabul edilir")) }
func ToUpper(s string) string
ToUpper returns s with all Unicode letters mapped to their upper case.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.ToUpper("Gopher")) }
func ToUpperSpecial(c unicode.SpecialCase, s string) string
ToUpperSpecial returns a copy of the string s with all Unicode letters mapped to their upper case using the case mapping specified by c.
package main import ( "fmt" "strings" "unicode" ) func main() { fmt.Println(strings.ToUpperSpecial(unicode.TurkishCase, "örnek iş")) }
func ToValidUTF8(s, replacement string) string
ToValidUTF8 returns a copy of the string s with each run of invalid UTF-8 byte sequences replaced by the replacement string, which may be empty.
func Trim(s, cutset string) string
Trim returns a slice of the string s with all leading and trailing Unicode code points contained in cutset removed.
package main import ( "fmt" "strings" ) func main() { fmt.Print(strings.Trim("¡¡¡Hello, Gophers!!!", "!¡")) }
func TrimFunc(s string, f func(rune) bool) string
TrimFunc returns a slice of the string s with all leading and trailing Unicode code points c satisfying f(c) removed.
package main import ( "fmt" "strings" "unicode" ) func main() { fmt.Print(strings.TrimFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsNumber(r) })) }
func TrimLeft(s, cutset string) string
TrimLeft returns a slice of the string s with all leading Unicode code points contained in cutset removed.
To remove a prefix, use TrimPrefix instead.
package main import ( "fmt" "strings" ) func main() { fmt.Print(strings.TrimLeft("¡¡¡Hello, Gophers!!!", "!¡")) }
func TrimLeftFunc(s string, f func(rune) bool) string
TrimLeftFunc returns a slice of the string s with all leading Unicode code points c satisfying f(c) removed.
package main import ( "fmt" "strings" "unicode" ) func main() { fmt.Print(strings.TrimLeftFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsNumber(r) })) }
func TrimPrefix(s, prefix string) string
TrimPrefix returns s without the provided leading prefix string. If s doesn't start with prefix, s is returned unchanged.
package main import ( "fmt" "strings" ) func main() { var s = "¡¡¡Hello, Gophers!!!" s = strings.TrimPrefix(s, "¡¡¡Hello, ") s = strings.TrimPrefix(s, "¡¡¡Howdy, ") fmt.Print(s) }
func TrimRight(s, cutset string) string
TrimRight returns a slice of the string s, with all trailing Unicode code points contained in cutset removed.
To remove a suffix, use TrimSuffix instead.
package main import ( "fmt" "strings" ) func main() { fmt.Print(strings.TrimRight("¡¡¡Hello, Gophers!!!", "!¡")) }
func TrimRightFunc(s string, f func(rune) bool) string
TrimRightFunc returns a slice of the string s with all trailing Unicode code points c satisfying f(c) removed.
package main import ( "fmt" "strings" "unicode" ) func main() { fmt.Print(strings.TrimRightFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsNumber(r) })) }
func TrimSpace(s string) string
TrimSpace returns a slice of the string s, with all leading and trailing white space removed, as defined by Unicode.
package main import ( "fmt" "strings" ) func main() { fmt.Println(strings.TrimSpace(" \t\n Hello, Gophers \n\t\r\n")) }
func TrimSuffix(s, suffix string) string
TrimSuffix returns s without the provided trailing suffix string. If s doesn't end with suffix, s is returned unchanged.
package main import ( "fmt" "strings" ) func main() { var s = "¡¡¡Hello, Gophers!!!" s = strings.TrimSuffix(s, ", Gophers!!!") s = strings.TrimSuffix(s, ", Marmots!!!") fmt.Print(s) }
A Builder is used to efficiently build a string using Write methods. It minimizes memory copying. The zero value is ready to use. Do not copy a non-zero Builder.
type Builder struct { // contains filtered or unexported fields }
package main import ( "fmt" "strings" ) func main() { var b strings.Builder for i := 3; i >= 1; i-- { fmt.Fprintf(&b, "%d...", i) } b.WriteString("ignition") fmt.Println(b.String()) }
func (b *Builder) Cap() int
Cap returns the capacity of the builder's underlying byte slice. It is the total space allocated for the string being built and includes any bytes already written.
func (b *Builder) Grow(n int)
Grow grows b's capacity, if necessary, to guarantee space for another n bytes. After Grow(n), at least n bytes can be written to b without another allocation. If n is negative, Grow panics.
func (b *Builder) Len() int
Len returns the number of accumulated bytes; b.Len() == len(b.String()).
func (b *Builder) Reset()
Reset resets the Builder to be empty.
func (b *Builder) String() string
String returns the accumulated string.
func (b *Builder) Write(p []byte) (int, error)
Write appends the contents of p to b's buffer. Write always returns len(p), nil.
func (b *Builder) WriteByte(c byte) error
WriteByte appends the byte c to b's buffer. The returned error is always nil.
func (b *Builder) WriteRune(r rune) (int, error)
WriteRune appends the UTF-8 encoding of Unicode code point r to b's buffer. It returns the length of r and a nil error.
func (b *Builder) WriteString(s string) (int, error)
WriteString appends the contents of s to b's buffer. It returns the length of s and a nil error.
A Reader implements the io.Reader, io.ReaderAt, io.Seeker, io.WriterTo, io.ByteScanner, and io.RuneScanner interfaces by reading from a string. The zero value for Reader operates like a Reader of an empty string.
type Reader struct { // contains filtered or unexported fields }
func NewReader(s string) *Reader
NewReader returns a new Reader reading from s. It is similar to bytes.NewBufferString but more efficient and read-only.
func (r *Reader) Len() int
Len returns the number of bytes of the unread portion of the string.
func (r *Reader) Read(b []byte) (n int, err error)
func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)
func (r *Reader) ReadByte() (byte, error)
func (r *Reader) ReadRune() (ch rune, size int, err error)
func (r *Reader) Reset(s string)
Reset resets the Reader to be reading from s.
func (r *Reader) Seek(offset int64, whence int) (int64, error)
Seek implements the io.Seeker interface.
func (r *Reader) Size() int64
Size returns the original length of the underlying string. Size is the number of bytes available for reading via ReadAt. The returned value is always the same and is not affected by calls to any other method.
func (r *Reader) UnreadByte() error
func (r *Reader) UnreadRune() error
func (r *Reader) WriteTo(w io.Writer) (n int64, err error)
WriteTo implements the io.WriterTo interface.
Replacer replaces a list of strings with replacements. It is safe for concurrent use by multiple goroutines.
type Replacer struct { // contains filtered or unexported fields }
func NewReplacer(oldnew ...string) *Replacer
NewReplacer returns a new Replacer from a list of old, new string pairs. Replacements are performed in the order they appear in the target string, without overlapping matches. The old string comparisons are done in argument order.
NewReplacer panics if given an odd number of arguments.
package main import ( "fmt" "strings" ) func main() { r := strings.NewReplacer("<", "<", ">", ">") fmt.Println(r.Replace("This is <b>HTML</b>!")) }
func (r *Replacer) Replace(s string) string
Replace returns a copy of s with all replacements performed.
func (r *Replacer) WriteString(w io.Writer, s string) (n int, err error)
WriteString writes s to w with all replacements performed.
The rule Title uses for word boundaries does not handle Unicode punctuation properly.
© Google, Inc.
Licensed under the Creative Commons Attribution License 3.0.
https://golang.org/pkg/strings/