In Go language, the string is an immutable chain of arbitrary bytes encoded with UTF-8 encoding. You are allowed to compare strings with each other using two different ways: 1. Using comparison operators: Go strings support comparison operators, i.e, ==, !=, >=, <=, <, >. Here, the == and != operator are used to check if the given strings are equal or not, and >=, <=, <, > operators are used to find the lexical order. The results of these operators are of Boolean type, meaning if the condition is satisfied it will return true, otherwise, return false. Example 1:
Output: Example 2:
Output: Slice: [Geeks Geeks gfg GFG for] Result 1: false Result 2: true Result 3: false Result 4: true Result 5: true Result 6: true2. Using Compare() method: You can also compare two strings using the built-in function Compare() provided by the strings package. This function returns an integer value after comparing two strings lexicographically. The return values are:
Syntax: func Compare(str1, str2 string) intExample:
Output: Article Tags :
Comparar o tamanho de strings é uma função comum na programação da linguagem C, pois ela permite verificar qual delas contém mais caracteres. Tal recurso é muito útil para ordenar dados. Comparar strings requer uma função especial; não use != ou ==.
|