Skip to content

Go strings API

Go strings package — functions for manipulating UTF-8 strings.

1 class · 10 methods

strings

10 methods

Package strings implements simple functions to manipulate UTF-8 encoded strings.

strings.Contains(s, substr string) bool

Return true if substr is within s.

Parameters

NameTypeDescription
sstringString to search.
substrstringSubstring to find.

Returns

bool

Example

go
package main
import ("fmt"; "strings")
func main() {
    fmt.Println(strings.Contains("hello", "ell"))  // true
    fmt.Println(strings.Contains("hello", "xyz"))  // false
}
strings.HasPrefix(s, prefix string) bool

Return true if s begins with prefix.

Parameters

NameTypeDescription
sstringString to check.
prefixstringPrefix to test.

Returns

bool

Example

go
strings.HasPrefix("hello.go", "hello")  // true
strings.HasPrefix("hello.go", ".go")    // false
strings.HasSuffix(s, suffix string) bool

Return true if s ends with suffix.

Parameters

NameTypeDescription
sstringString to check.
suffixstringSuffix to test.

Returns

bool

Example

go
strings.HasSuffix("hello.go", ".go")  // true
strings.HasSuffix("hello.go", "hello")  // false
strings.Index(s, substr string) int

Return the index of the first instance of substr in s, or -1 if not present.

Parameters

NameTypeDescription
sstringString to search.
substrstringSubstring to find.

Returns

int

Example

go
strings.Index("hello", "ll")  // 2
strings.Index("hello", "z")   // -1
strings.Split(s, sep string) []string

Split s into substrings separated by sep. Returns []string.

Parameters

NameTypeDescription
sstringString to split.
sepstringSeparator.

Returns

[]string

Example

go
strings.Split("a,b,c", ",")  // ["a", "b", "c"]
strings.Split("hello", "")    // ["h","e","l","l","o"]
strings.Join(elems []string, sep string) string

Concatenate the elements of elems with sep between each.

Parameters

NameTypeDescription
elems[]stringStrings to join.
sepstringSeparator.

Returns

string

Example

go
strings.Join([]string{"a", "b", "c"}, "-")  // "a-b-c"
strings.Join([]string{"x"}, ",")            // "x"
strings.Replace(s, old, new string, n int) string

Return s with the first n non-overlapping instances of old replaced by new. n < 0 means no limit.

Parameters

NameTypeDescription
sstringSource string.
oldstringSubstring to replace.
newstringReplacement.
nintMax replacements (-1 for all).

Returns

string

Example

go
strings.Replace("a-b-c", "-", "_", 1)   // "a_b-c"
strings.Replace("a-b-c", "-", "_", -1)  // "a_b_c"
strings.ToUpper(s string) string

Return s with all Unicode letters mapped to upper case.

Parameters

NameTypeDescription
sstringSource string.

Returns

string

Example

go
strings.ToUpper("hello")  // "HELLO"
strings.ToUpper("café")   // "CAFÉ"
strings.ToLower(s string) string

Return s with all Unicode letters mapped to lower case.

Parameters

NameTypeDescription
sstringSource string.

Returns

string

Example

go
strings.ToLower("HELLO")  // "hello"
strings.ToLower("CAFÉ")   // "café"
strings.Trim(s, cutset string) string

Return s with all leading and trailing characters in cutset removed.

Parameters

NameTypeDescription
sstringSource string.
cutsetstringCharacters to trim.

Returns

string

Example

go
strings.Trim("  hi  ", " ")   // "hi"
strings.Trim("xxhelloxx", "x")  // "hello"