You are reading the article How Does String Functions Work In Go Language? updated in September 2023 on the website Lanphuongmhbrtower.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How Does String Functions Work In Go Language?
Introduction to Go StringsString in case of go language is different as compared to another language. The string in the case of go language is variable width with sequence, go language string main power is relay on the various functions of the string in the go language. We can perform all the basic operation of string in go language like we can add, compare two string, we can check any letter inside the string we can check the length of any string, generally, in go language, any string will be denoted by double quotes symbol (“”), there is a function in go language for a string which reduces our efforts.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
How Does String Functions Work in Go Language?In go language, if we want to understand the working of the string function then we can take some simple examples of the format.
Import ( s "string" ) s.name_system_defined_functions()In the above flow, we have two things to discuss,
Importing the string, here we need to first import the string package for use of the all available functions for the string.
s.name_system_defined_function: Here we can use any function, basically a system defined function on the instance of imported string packages “s” .Example s.Contains() ,s.Count() etc .
Types of String Function in Go LanguageBelow are the types of Go Strings:
1. LenThis function allows us to find the length of the string or simple word numbers of letters inside any string. In the below example we are calculating the length of the string.
package main import ( "fmt" ) var p = fmt.Println func main() { var name ="Ranjan" fmt.Println("Length of the name is:", len(name)) }Output:
2. ToUpperThis function will be used to convert all letters of the string from small letters to capital letters.
Code:
package main import ( "fmt" s "strings" ) var p = fmt.Println func main() { var name ="Ranjan" fmt.Println("Upper case of string name:", s.ToUpper(name)) }Output:
3. ToLowerThis function will be used to convert all letters of the string from capital letters to small letters.
Code:
package main import ( "fmt" s "strings" ) var p = fmt.Println func main() { var name ="Ranjan" fmt.Println("Length of the name is:", s.ToLower(name)) }Output:
4. SplitThis function is used for splitting strings, this function can take two parameters first is the string which we are going to split and the second function is the delimiter that comes between each letter after splitting.
Code:
package main import ( "fmt" s "strings" ) var p = fmt.Println func main() { var name ="Ranjan" fmt.Println("String after splitting:", s.Split(name,"")) }Output:
5. ReplaceWith this function we can replace any letter from another letter inside any string, this function will take three argument, first is the string in which we want to do the replacement, second is the letter which we want to replace and the third argument is the letter which we want to be put inside the string. We can pass another numeric argument to indicate how many letters we want to replace if match.
Code:
package main import ( "fmt" s "strings" ) var p = fmt.Println func main() { var name ="Ranjan" fmt.Println("New string after replacing:", s.Replace(name,"R","M",1)) fmt.Println("New string after replacing:", s.Replace(name,"a","k",2)) }Output:
6. RepeatIn case if we want to repeat some string or letter a defined number of times we can use this string function. See in the below example we are repeating names three times.
Code:
package main import ( "fmt" s "strings" ) var p = fmt.Println func main() { var name ="Ranjan" fmt.Println("Repeat string name for 3 times:", s.Repeat(name,3)) } 7. JoinJoin two strings or letters and create another string, in this function we can pass the string and the delimiter which will come between each letter or string.
Code:
package main import ( "fmt" s "strings" ) var p = fmt.Println func main() { fmt.Println("Joining names together:", s.Join([]string{"Ranjan", "Kumar","Pandey"}, " ")) }Output:
8. IndexWith the help of this function, we are retrieving the position of the letter. In the below example we are getting the position of the various letters inside the given string.
Please see the below example.
Code:
package main import ( "fmt" s "strings" ) var p = fmt.Println func main() { fmt.Println("The index of letter j is :", s.Index("Ranjan", "j")) fmt.Println("The index of letter R is:", s.Index("Ranjan", "R")) }Output:
9. HasSuffixThis string function allows us to check if the string ends with a given letter or a combination of the letters. In the below example we are checking if various string patterns are suffixed for strings.
Please see the below example.
Code:
package main import ( "fmt" s "strings" ) var p = fmt.Println func main() { fmt.Println("Checking for the suffix an:", s.HasSuffix("Ranjan", "an")) fmt.Println("Checking for the suffix Ra:", s.HasSuffix("Ranjan", "Ra")) fmt.Println("Checking for the suffix Rk:", s.HasSuffix("Ranjan", "Rk")) } 10. HasPrefixThis string function allows us to check if the string starts with a given letter or a combination of the letters. In the below example we are checking if various string patterns are prefixes for strings.
Code:
package main import ( "fmt" s "strings" ) var p = fmt.Println func main() { fmt.Println("Checking for the prefix Ra:", s.HasPrefix("Ranjan", "Ra")) fmt.Println("Checking for the prefix Ra:", s.HasPrefix("Ranjan", "Rn")) fmt.Println("Checking for the prefix Rk:", s.HasPrefix("Ajay", "Aj")) }Output:
11. CountWith the help of the string function Count in Go language, we can check the total number of letters inside any string. In the below example we are checking the number of counts for various letters inside the strings.
Code:
package main import ( "fmt" s "strings" ) var p = fmt.Println func main() { fmt.Println("Counting the number of letter R:", s.Count("Ranjan", "R")) fmt.Println("Counting the number of letter n:", s.Count("Ranjan", "n")) fmt.Println("Counting the number of letter a:", s.Count("Ajay", "a")) }Output:
12. ContainsThe Contains function allows us to check if the given letter exists inside any string. In the below example we are checking various letters existence inside strings.
Code:
package main import ( "fmt" s "strings" ) var p = fmt.Println func main() { fmt.Println("Check if the letter R inside string or not:", s.Contains("Ranjan", "R")) fmt.Println("Check if the letter k inside string or not:", s.Contains("Ranjan", "k")) fmt.Println("Check if the letter a inside string or not:", s.Contains("Ajay", "a")) }Output:
ConclusionFrom this example we learned some basic concepts of the string in go language, we also learned about the function of the string in go language. We learned the working string function with some important examples along with various types of the functions of string in go language.
Recommended ArticleThis is a guide to Go Strings. Here we discuss the introduction and working of string function in Go Language along with various types and examples. You can also go through our other suggested articles to learn more –
You're reading How Does String Functions Work In Go Language?
Update the detailed information about How Does String Functions Work In Go Language? on the Lanphuongmhbrtower.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!