Comparing Strings in Golang

The strings.Compare() is a built-in function in the strings package of Golang that is used to compare two strings in lexicographical order. It returns an integer value after comparing two strings. Syntax The syntax for the strings.Compare() function is as follows: Where, Parameters The strings.Compare() function takes two parameters: Return Value The strings.Compare() function returns …

Read more

Golang String Count() – Syntax, Examples

The Count() Function in the Golang strings package is used to count the number of instances of a specified substring in the given string. It returns an integer value. Count() Syntax The Count() function syntax: Count() Parameters: The Count() function takes two parameters Count() Return Value: The strings package Count() function returns an int value. …

Read more

Golang String Index() Function

Index() function in the Golang strings package returns the index of the first instance of the substring in the given string. If the given string doesn’t contain a substring, it returns -1. Index() Syntax The syntax for Index() function is: Index() Parameters The Index() function takes two string arguments s1 is the string substr is …

Read more

Golang String HasSuffix() Function

HasSuffix() function in the Golang strings package checks if the given string ends with a specified suffix. It returns true if the given string has a specified suffix else returns false. HasSuffix() Syntax The syntax for HasSuffix() function is: HasSuffix() Parameters The HasSuffix() function takes two string arguments s1 is the string suffix is the …

Read more

Golang String HasPrefix() Function

HasPrefix() function in the Golang strings package checks if the given string begins with a specified prefix. It returns true if the given string has a specified prefix else returns false. HasPrefix() Syntax The syntax for HasPrefix() function is: HasPrefix() Parameters: The HasPrefix() function takes two string arguments s1 is the string the prefix is …

Read more

Golang String Fields() Function

Fields() function in the Golang strings package splits the string around each instance of one or more consecutive white space characters as defined by Unicode.IsSpace. It returns a slice of substrings of the given string. If the given string is an empty string, it will return an empty slice. Fields() Syntax The syntax for Fields() …

Read more

Golang String Cut Function

Cut() Function in Golang strings package is used to cut the slices of the given string around the first instance of sep. It returns the text before and after the sep and the boolean value if the sep appears in the given string. If the sep is not present in the given string, the Cut() …

Read more

Golang String EqualFold() function

EqualFold() function in the Golang strings package is used to check if two strings are equal. It returns a true value if two strings are equal else false. Here strings are interpreted as UTF-8 strings and comparison is performed under Unicode case-folding. The string comparison using the EqualFold() function is not case-sensitive. EqualFold() Syntax The …

Read more

Golang String ContainsRune()

ContainsRune() Function in Golang strings package is used to check the given string contains the specified rune in it or not. It returns true if the Unicode code point r in rune are present in the given string else returns false. ContainsRune() Syntax The syntax for ContainsRune() function is: ContainsRune() Parameters: The ContainsRune() function take …

Read more

Golang String ContainsAny

Golang strings package ContainsAny() is a built-in function used to check if the given Unicode characters are present in the string. It returns true if the characters in the second argument in ContainsAny() function is present in the given string else returns false. ContainsAny() method is different from string.Contains() method as it returns true if …

Read more