Found 545 Articles for Go Programming

Replace() vs ReplaceAll() in Golang

Syed Abeed
Updated on 10-Mar-2022 07:41:41
ReplaceAll() function in Golang replaces all the occurrences of a given substring with a new value. In contrast, Replace() function is used to replace only some characters in a string with a new value. It replaces only a specified "n" occurrences of the substring.SyntaxThe syntax of ReplaceAll() is as follows −func ReplaceAll(s, old, new string) stringWhere, s is the given stringold is the string which we want to replace, andnew is string which will replace the old string.Example 1Let us consider the following example −package main import (    "fmt"    "strings" ) func main() {    // Initializing ... Read More

How to check if a string ends with a specified Suffix string in Golang?

Syed Abeed
Updated on 10-Mar-2022 07:37:41
The HasSuffix() function of string class in Golang is used to check whether a given string ends with a specified Suffix string or not. It returns True if the given string ends with the specified Suffix string; otherwise it returns False.HasSuffix() and HasPrefix() check if a string ends or starts with a particular set of characters, respectively.Syntaxfunc HasSuffix(s, prefix string) boolWhere x is the given string. It returns a Boolean value.Example 1In this example, we are going to use HasSuffix() with an if condition to check wheter two defined string variables are ending with the same set of characters or not.package main import ... Read More

How to check if a string starts with a specified Prefix string in Golang?

Syed Abeed
Updated on 10-Mar-2022 07:26:23
The HasPrefix() function of string class in Golang is used to check whether a given string begins with a specified Prefix string or not. It returns True if the given string begins with the specified prefix string; otherwise it returns False.Syntaxfunc HasPrefix(s, prefix string) boolWhere x is the given string. It returns a Boolean value.ExampleIn this example, we are going to use HasPrefix() along with an if condition to check wheter the two defined variables are starting with the same Prefix string or not.package main import (    "fmt"    "strings" ) func main() {        // Initializing ... Read More

How to Replace characters in a Golang string?

Syed Abeed
Updated on 10-Mar-2022 07:14:14
The strings package of Golang has a Replace() function which we can use to replace some characters in a string with a new value. It replaces only a specified "n" occurrences of the substring.In addition to Replace(), there is a ReplaceAll() function that replaces all the occurrences of a given substring with a new value.Syntaxfunc Replace(s, old, new string, n int) stringWhere, s is the given stringold is the string which we want to replacenew is the string which will replace the old stringn represents the number of characters which we want to replace in the given string.ExampleThe following example demonstrates ... Read More

How to use the Fields() function in Golang?

Syed Abeed
Updated on 10-Mar-2022 07:08:43
The strings package of Golang provides a Fields() method, which can be used to split a string around one or more instances of consecutive whitespace characters.The Fields() function splits a given string into substrings by removing any space characters, including newlines. And it treats multiple consecutive spaces as a single space.Syntaxfunc Fields(s string) []stringWhere s is the string parameter.ExampleLet us consider the following example −package main import (    "fmt"    "strings" ) func main() {    // Initializing the Strings    string1 := " The Golang Programming Language "    // Display the Strings    fmt.Println("Input String:", string1) ... Read More

What is the EqualFold function in Golang?

Syed Abeed
Updated on 10-Mar-2022 07:00:55
The EqualFold() function in Golang is an inbuilt function of strings package which is used to check whether the given strings (UTF-8 strings) are equal. The comparison is not case-sensitive. It accepts two string parameters and returns True if both the strings are equal under Unicode case-folding (i.e., case-insensitive), False otherwise.Syntaxfunc EqualFold(s, t string) boolWhere s and t are strings. It returns a Boolean value.ExampleThe following example demonstrates how to use EqualFold() in a Go program −package main import (    "fmt"    "strings" ) func main() {    // Intializing the Strings    R := "Welcome to Tutorialspoint" ... Read More

How to count the number of repeated characters in a Golang String?

Syed Abeed
Updated on 10-Mar-2022 06:57:24
Count() is a built-in function is Golang that can be used to count the number of non-overlapping instances of a character/string in a string.Syntaxfunc Count(s, sep string) intWhere, s – Original Stringsep – Substring which we want to count.It returns an Integer value.ExampleThe following example demonstrates how you can use the Count() function in a Go program.package main import (    "fmt"    "strings" ) func main() {    // Initializing the Strings    x := "Golang Programming Language"    y := "Language"       // Display the Strings    fmt.Println("First String:", x)    fmt.Println("Second String:", y)   ... Read More

How to use ContainsAny() function in Golang?

Syed Abeed
Updated on 14-Mar-2022 05:41:25
Golang has a built-in string function called ContainsAny() that we can use to check whether a specified string is present in a given string or not.ContainsAny() is completely different from Contains().Contains() is used to detect if a string contains a substring.ContainsAny() is used to detect if a string contains any characters in the provided string. Even if one character of the specified string is present in the original given string, then it returns True, else False.Syntaxfunc ContainsAny(s, chars string) boolWhere, s – Original Stringchars string – Substring where we define the string or characters.It returns a Boolean value.ExampleContainsAny() is case-sensitive, ... Read More

How to compare two strings in Golang?

Syed Abeed
Updated on 10-Mar-2022 08:53:01
Golang has a built-in string function called Compare() that we can use to compare two strings. Here strings are compared using the lexicographical order.Syntaxfunc Compare(a, b string) intReturn TypesIf the strings (a == b), it returns 0.If the strings (a > b), then it returns 1If the strings (a < b), then it returns -1ExampleLet us consider the following example −package main // importing fmt and strings import (    "fmt"    "strings" ) func main() {    // Intializing the variables    var a1 = "a"    var a2 = "b"    var a3 = "welcome"    var a4 ... Read More

How to use Contains() function in Golang?

Syed Abeed
Updated on 10-Mar-2022 06:42:28
Golang has a set of built-in string functions that we can utilize to perform different types of operations on string data. Contains() is such a function that can be used to search whether a particular text/string/character is present in a given string. If the text/string/character is present in the given string, then it returns True; else it returns False.Syntaxfunc Contains(s, substr string) boolWhere, s is the original string and substr is the string which is to be checked with the original string.ExampleThe following example demonstrates how Contains() works −package main // importing fmt and strings import (    "fmt"   ... Read More
Advertisements