strings.Compare() Function in Golang with Examples Last Updated : 21 Apr, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report The Compare() function is an inbuilt function in the Golang programming language which is used to compare two strings. It is used to compare two strings in lexicographical order (order in which the words are arranged alphabetically, similar to the way we search words in dictionary) or to find out if the strings are equal or not. It returns an integer value as follows: Syntax: func Compare(s1, s2 string) int Returns 0 if the strings are equal (s1==s2) Returns 1 if string 1 is greater than string 2 (s1 > s2) Returns -1 is string 1 is lesser than string 2 (s1 < s2) Example 1: C // Golang program to illustrate the use of // the strings.Compare() Function package main import ( "fmt" "strings" ) func main() { var s1 = "Geeks" var s2 = "GeeksforGeeks" var s3 = "Geeks" // using the function fmt.Println(strings.Compare(s1, s2)) fmt.Println(strings.Compare(s2, s3)) fmt.Println(strings.Compare(s3, s1)) } Output: -1 1 0 Explanation: The first output comes -1 as the first string is "Geeks" which is lexicographically lesser than the second string "GeeksforGeeks". The second output comes 1 as the first string is "GeeksforGeeks" which is lexicographically greater than the second string "Geeks". The third output comes as 0 as the first string is "Geeks" which is equal to the second string "Geeks". Example 2: C // Golang program to illustrate the use of // the strings.Compare() Function package main import ( "fmt" "strings" ) func main() { var s1 = "apple" var s2 = "Apple" var s3 = "Apricot" // using the function fmt.Println(strings.Compare(s1, s2)) fmt.Println(strings.Compare(s2, s3)) fmt.Println(strings.Compare(s3, s1)) } Output: 1 -1 -1 Explanation: The first output comes 1 as the first string is "apple" which is lexicographically greater than the second string "Apple" as the characters are compared sequentially from left to right using the Unicode Character Set and the ASCII value of 'a' is 97 and that of 'A' is 65. Therefore apple is greater than Apple. The second output comes -1 as the first string is "Apple" which is lexicographically lesser than the second string "Apricot". The third output comes as -1 as the first string is "Apricot" which is lexicographically lesser than the second string "apple" as 'A' is lesser than 'a'. Comment More infoAdvertise with us Next Article strings.ContainsAny() Function in Golang with Examples P prakhar7 Follow Improve Article Tags : Go Language Golang-String Similar Reads strings.Contains Function in Golang with Examples strings.Contains Function in Golang is used to check the given letters present in the given string or not. If the letter is present in the given string, then it will return true, otherwise, return false. Syntax: func Contains(str, substr string) bool Here, str is the original string and substr is t 2 min read strings.ContainsRune() Function in Golang with Examples The Unicode, which is a superset of ASCII and contains all the characters present in the world's writing system, is the character set that's being followed right now. Each character in the Unicode system is uniquely identified by a Unicode code point, which is referred to as runes in Golang. To know 2 min read strings.ContainsAny() Function in Golang with Examples ContainsAny function is used to check whether any Unicode code points in chars are present in the string. It is an inbuilt function used to find if a specified string exists in the string if found it will return true or else false. Syntax: func ContainsAny(str, charstr string) bool Here, the first p 2 min read strings.Map() Function in Golang With Examples strings.Map() Function in Golang is used to return a copy of the string given string with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the string with no replacement. So whenever the user wants to make changes in a 2 min read strings.EqualFold() Function in Golang With Examples strings.EqualFold() Function in Golang reports whether s and t, interpreted as UTF-8 strings, are equal under Unicode case-folding, which is a more general form of case-insensitivity. Syntax: func EqualFold(s1, s2 string) bool Here, s1 and s2 are strings. Return Value: It returns the boolean value 1 min read strings.Join() Function in Golang With Examples strings.Join() Function in Golang concatenates all the elements present in the slice of string into a single string. This function is available in the string package. Syntax: func Join(s []string, sep string) string Here, s is the string from which we can concatenate elements and sep is the separato 1 min read Like