Strings and strconv Package
In this lesson, you'll study strings, strconv package, and the functions supported by them.
We'll cover the following
- Prefixes and suffixes
- Testing whether a string contains a substring
- Indicating the index a substring or character in a string
- Replacing substring
- Counting occurrences of a substring
- Repeating a string
- Changing the case of a string
- Trimming a string
- Splitting a string
- Joining over a slice
- Reading from a string
- Conversion to and from a string
- Try it yourself
Strings are a basic data structure, and every language has a number of predefined functions for manipulating strings. In Go, these are gathered in a package, strings
. We’ll discuss below some very useful functions one by one.
Prefixes and suffixes
HasPrefix
tests whether the string s
begins with a prefix prefix
:
strings.HasPrefix(s, prefix string) bool
HasSuffix
tests whether the string s
ends with a suffix suffix
:
strings.HasSuffix(s, suffix string) bool
The following program implements these functions:
Get hands-on with 1400+ tech skills courses.