Working with string cases
When working with textual data, problems related to string cases arise often. Should a text search be case-sensitive or case-insensitive? How do we convert a string to lowercase or uppercase? In this section, we will look at some recipes to deal with these common problems in a portable way.
How to do it...
- Convert strings to uppercase and lowercase using the
strings.ToUpper
andstrings.ToLower
functions, respectively. - When dealing with text in languages with special uppercase/lowercase mappings (such as Turkish, where “İ” is the uppercase version of “I”), use
strings.ToUpperSpecial
andstrings.ToLowerSpecial
- To convert text to uppercase for use in titles, use
strings.ToTitle
- To compare strings lexicographically, use comparison operators
- To test the equivalence of strings ignoring case, use
strings.EqualFold
How it works...
Converting a string to uppercase or lowercase is easy:
greet...