Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Types in Go
  • Go Keywords
  • Go Control Flow
  • Go Functions
  • GoLang Structures
  • GoLang Arrays
  • GoLang Strings
  • GoLang Pointers
  • GoLang Interface
  • GoLang Concurrency
Open In App
Next Article:
string package in Golang
Next article icon

string package in Golang

Last Updated : 15 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 check 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: 

Go
// 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:

Go
// 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

Example 3 :

Go
package main  import (     "fmt"     "strings" )  func main() {     //fmt.Println("Bhavya")     var c string     c = "bhavya"     d := strings.Replace(c, "h", "v", 1)     e := strings.Contains(c, "bha")     f := strings.Count(c, "a")     g := strings.Repeat(c, 5)     h := strings.Split(c, "a")     i := strings.ToLower(c)     j := strings.ToUpper(c)     k := strings.ToTitle(c)     l := strings.HasPrefix(c, "BHa")     m := strings.HasSuffix(c, "vy")     n := strings.ReplaceAll(c, "a", "b")     o := strings.Compare(c, "bhavya")     p := strings.Trim(c, "a")     r := strings.EqualFold(c, "BHAVYA")     s := strings.IndexAny(c, "b")     t := strings.Index(c, "a")     u := strings.EqualFold("bhavya", "BHAVYA")     v := strings.SplitN("b.h*a.v*y.a", "*", 3)     fmt.Println(d, e, f, g, h, i, j, k, l, m, n, o, p,r, s, t, u, v) } 

Output
bvavya true 2 bhavyabhavyabhavyabhavyabhavya [bh vy ] bhavya BHAVYA BHAVYA false false bhbvyb 0 bhavy true 0 2 true [b.h a.v y.a]


 


Next Article
string package in Golang

A

ankita_saini
Improve
Article Tags :
  • Go Language
  • Golang-String

Similar Reads

    strconv package in Golang
    Go language provides a strconv package that implements conversions to and from string representations of basic data types. To access the functions of the strconv package you need to import the strconv package in your program with the help of the import keyword. .strconv-golang-table { border-collaps
    5 min read
    Packages in Golang
    Packages are the most powerful part of the Go language. The purpose of a package is to design and maintain a large number of programs by grouping related features together into single units so that they can be easy to maintain and understand and independent of the other package programs. This modula
    5 min read
    fmt Package in GoLang
    Prerequisite: Packages in GoLang and Import in GoLang  Technically defining, a package is essentially a container of source code for some specific purpose. Packages are very essential as in all programs ranging from the most basic programs to high-level complex codes, these are used. A package ensur
    13 min read
    How to Split a String in Golang?
    In Go language, strings differ from other languages like Java, C++, and Python. A string in Go is a sequence of variable-width characters, with each character represented by one or more bytes using UTF-8 encoding. In Go, you can split a string into a slice using several functions provided in the str
    3 min read
    How to Replace Characters in Golang String?
    In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go strings, you are allowed to replace characters in the given string usin
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences