Skip to content
Related Articles

Related Articles

string package in Golang
  • Last Updated : 28 Jul, 2020

Go language provides a string package that holds different types of functions to manipulate UTF-8 encoded strings. To access the function of the string package you need to import a string package in your program with the help of the import keyword.

FunctionDescription
func CompareThis function is used to return an integer comparing two strings lexicographically.
func ContainsThis function is used to check whether substr is within s or not.
func ContainsAnyThis function is used to check whether any Unicode code points in chars are within s or not.
func ContainsRuneThis function is used to check whether the Unicode code point r is within s or not.
func CountThis function is used to count the number of non-overlapping instances of substr in given string s.
func EqualFoldThis function is used to check whether s and t, interpreted as UTF-8 strings, are equal under Unicode case-folding, which is a more general form of case-insensitivity or not.
func FieldsThis function is used to splits the given 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.
func FieldsFuncThis function is used to splits the string s at each run of Unicode code points c satisfying f(c) and returns an array of slices of s.
func HasPrefixThis function is used to cehck whether the string s begins with prefix or not.
HasSuffixThis function is used to check whether the string s ends with suffix or not.
func IndexThis function is used to returns the index of the first instance of substr in s or -1 if substr is not present in s.
func IndexAnyThis function is used to 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.
func IndexByteThis function is used to returns the index of the first instance of c in s, or -1 if c is not present in s.
func IndexFuncThis function is used to returns the index into s of the first Unicode code point satisfying f(c) or -1 if none do.
func IndexRuneThis function is used to returns the index of the first instance of the Unicode code point r, or -1 if rune is not present in s.
func JoinThis function is used to concatenate the elements of its first argument to create a single string.
func LastIndexThis function is used to returns the index of the last instance of substr in s, or -1 if substr is not present in s.
func LastIndexAnyThis function is used to 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.
func LastIndexByteThis function is used to returns the index of the last instance of c in s, or -1 if c is not present in s.
func LastIndexFuncThis function is used to returns the index into s of the last Unicode code point satisfying f(c), or -1 if none do.
func MapThis function is used to returns a copy of the string s with all its characters modified according to the mapping function.
func RepeatThis function is used to returns a new string consisting of count copies of the string s.
func ReplaceThis function is used to returns a copy of the string s with the first n non-overlapping instances of old replaced by new.
func ReplaceAllThis function is used to returns a copy of the string s with all non-overlapping instances of old replaced by new.
func SplitThis function is used to slices s into all substrings separated by sep and returns a slice of the substrings between those separators.
func SplitAfterThis function is used to slices s into all substrings after each instance of sep and returns a slice of those substrings.
func SplitAfterNThis function is used to slices s into substrings after each instance of sep and returns a slice of those substrings.
func SplitNThis function is used to slices s into substrings separated by sep and returns a slice of the substrings between those separators.
func TitleThis function is used to returns a copy of the string s with all Unicode letters that begin words mapped to their Unicode title case.
func ToLowerThis method is used to returns s with all Unicode letters mapped to their lower case.
func ToLowerSpecialThis function is used to returns a copy of the string s with all Unicode letters mapped to their lower case using the case mapping specified by c.
func ToTitleThis function is used to returns a copy of the string s with all Unicode letters mapped to their Unicode title case.
func ToTitleSpecialThis function is used to returns a copy of the string s with all Unicode letters mapped to their Unicode title case, giving priority to the special casing rules.
func ToUpperThis function is used to returns s with all Unicode letters mapped to their upper case.
func ToUpperSpecialThis function is used to returns a copy of the string s with all Unicode letters mapped to their upper case using the case mapping specified by c.
func ToValidUTF8This function is used to 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 TrimThis function is used to returns a slice of the given string with all leading and trailing Unicode code points contained in cutset removed.
func TrimFuncThis function is used to returns a slice of the given string with all leading and trailing Unicode code points c satisfying f(c) removed.
func TrimLeftThis method is used to returns a slice of the given string with all leading Unicode code points contained in cutset removed.
func TrimLeftFuncThis function is used to returns a slice of the given string with all leading Unicode code points c satisfying f(c) removed.
func TrimPrefixThis function is used to returns s without the provided leading prefix string. If s doesn’t start with prefix, s is returned unchanged.
func TrimRightThis function is used to returns a slice of the given string, with all trailing Unicode code points contained in cutset removed.
func TrimRightFuncThis function is used to returns a slice of the given string with all trailing Unicode code points c satisfying f(c) removed.
func TrimSpaceThis function is used to returns a slice of given the string, with all leading and trailing white space removed, as defined by Unicode.
func TrimSuffixThis function is used to returns s without the provided trailing suffix string.

type Builder

MethodDescription
func (*Builder) CapThis method is used to returns the capacity of the builder’s underlying byte slice.
func (*Builder) GrowThis method is used to grows b’s capacity if necessary, to guarantee space for another n bytes.
func (*Builder) LenThis method is used to returns the number of accumulated bytes.
func (*Builder) ResetThis method is used to resets the Builder to be empty.
func (*Builder) StringThis method is used to returns the accumulated string.
func (*Builder) WriteThis method is used to appends the contents of p to b’s buffer.
func (*Builder) WriteByteThis method is used to appends the byte c to b’s buffer.
func (*Builder) WriteRuneThis method is used to appends the UTF-8 encoding of Unicode code point r to b’s buffer.
func (*Builder) WriteStringThis method is used to appends the contents of str to b’s buffer. It returns the length of str and a nil error.

type Reader

MethodDescription
func NewReaderThis function is used to returns a new Reader reading from s.
func (*Reader) LenThis method is used to returns the number of bytes of the unread portion of the string.
func (*Reader) ResetThis method is used to resets the Reader to be reading from s.
func (*Reader) SeekThis method is used to implements the io.Seeker interface.
func (*Reader) SizeThis method is used to returns the original length of the underlying string.
func (*Reader) WriteToThis method is used to implements the io.WriterTo interface.

type Replacer

MethodDescription
func NewReplacerThis function is used to returns a new Replacer from a list of old, new string pairs.
func (*Replacer) ReplaceThis method is used to returns a copy of s with all replacements performed.
func (*Replacer) WriteStringThis method is used to writes s to w with all replacements performed.

Example 1:




// Golang program to illustrate the use of 
// the strings.Compare() Function 
package main 
    
import
    "fmt"
    "strings"
    
func main() { 
    
    var r1 = "Geeks"
    var r2 = "GeeksforGeeks"
    var r3 = "Geeks"
    
    // using the function 
    fmt.Println(strings.Compare(r1, r2)) 
    fmt.Println(strings.Compare(r2, r3)) 
    fmt.Println(strings.Compare(r3, r1)) 
    

Output:

-1
1
0

Example 2:




// Golang program to illustrate 
// the strings.IndexAny() Function 
package main 
  
import
    "fmt"
    "strings"
  
// Main function 
func main() { 
  
    // using the function 
    fmt.Println(strings.IndexAny("Hey GFG?", "y")) 
      

Output:

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :