strings
10 methodsPackage strings implements simple functions to manipulate UTF-8 encoded strings.
strings.Contains(s, substr string) boolReturn true if substr is within s.
Parameters
| Name | Type | Description |
|---|---|---|
| s | string | String to search. |
| substr | string | Substring to find. |
Returns
bool
Example
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) boolReturn true if s begins with prefix.
Parameters
| Name | Type | Description |
|---|---|---|
| s | string | String to check. |
| prefix | string | Prefix to test. |
Returns
bool
Example
strings.HasPrefix("hello.go", "hello") // true
strings.HasPrefix("hello.go", ".go") // falsestrings.HasSuffix(s, suffix string) boolReturn true if s ends with suffix.
Parameters
| Name | Type | Description |
|---|---|---|
| s | string | String to check. |
| suffix | string | Suffix to test. |
Returns
bool
Example
strings.HasSuffix("hello.go", ".go") // true
strings.HasSuffix("hello.go", "hello") // falsestrings.Index(s, substr string) intReturn the index of the first instance of substr in s, or -1 if not present.
Parameters
| Name | Type | Description |
|---|---|---|
| s | string | String to search. |
| substr | string | Substring to find. |
Returns
int
Example
strings.Index("hello", "ll") // 2
strings.Index("hello", "z") // -1strings.Split(s, sep string) []stringSplit s into substrings separated by sep. Returns []string.
Parameters
| Name | Type | Description |
|---|---|---|
| s | string | String to split. |
| sep | string | Separator. |
Returns
[]string
Example
strings.Split("a,b,c", ",") // ["a", "b", "c"]
strings.Split("hello", "") // ["h","e","l","l","o"]strings.Join(elems []string, sep string) stringConcatenate the elements of elems with sep between each.
Parameters
| Name | Type | Description |
|---|---|---|
| elems | []string | Strings to join. |
| sep | string | Separator. |
Returns
string
Example
strings.Join([]string{"a", "b", "c"}, "-") // "a-b-c"
strings.Join([]string{"x"}, ",") // "x"strings.Replace(s, old, new string, n int) stringReturn s with the first n non-overlapping instances of old replaced by new. n < 0 means no limit.
Parameters
| Name | Type | Description |
|---|---|---|
| s | string | Source string. |
| old | string | Substring to replace. |
| new | string | Replacement. |
| n | int | Max replacements (-1 for all). |
Returns
string
Example
strings.Replace("a-b-c", "-", "_", 1) // "a_b-c"
strings.Replace("a-b-c", "-", "_", -1) // "a_b_c"strings.ToUpper(s string) stringReturn s with all Unicode letters mapped to upper case.
Parameters
| Name | Type | Description |
|---|---|---|
| s | string | Source string. |
Returns
string
Example
strings.ToUpper("hello") // "HELLO"
strings.ToUpper("café") // "CAFÉ"strings.ToLower(s string) stringReturn s with all Unicode letters mapped to lower case.
Parameters
| Name | Type | Description |
|---|---|---|
| s | string | Source string. |
Returns
string
Example
strings.ToLower("HELLO") // "hello"
strings.ToLower("CAFÉ") // "café"strings.Trim(s, cutset string) stringReturn s with all leading and trailing characters in cutset removed.
Parameters
| Name | Type | Description |
|---|---|---|
| s | string | Source string. |
| cutset | string | Characters to trim. |
Returns
string
Example
strings.Trim(" hi ", " ") // "hi"
strings.Trim("xxhelloxx", "x") // "hello"