130

How do I convert a string to a lower case representation?

I feel that there must be built-in function for it, but I just can't find it.

I did find a ToLower in "unicode/letter", but it only works for one rune at a time.

Nik
  • 2,885
  • 2
  • 25
  • 25
oers
  • 18,436
  • 13
  • 66
  • 75

2 Answers2

172

Yes there is, check the strings package.

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.ToLower("Gopher"))
}
Aor
  • 185
  • 11
AurA
  • 12,135
  • 7
  • 46
  • 63
  • thx a lot I completely missed the strings package :) and googling didn't bring up anything – oers May 02 '12 at 10:16
  • 4
    While the answer is correct, links tend to perish and adding a code sample that illustrates the solution is preferable. – ereOn Dec 09 '16 at 16:00
54

If you happen to be too lazy to click through to the strings package, here's example code:

strings.ToLower("Hello, WoRLd") // => "hello, world"

If you need to handle a Unicode Special Case like Azeri or Turkish, you can use ToLowerSpecial:

strings.ToLowerSpecial(unicode.TurkishCase, "Hello, WoRLd") // => "hello, world"
Ryan Endacott
  • 8,772
  • 4
  • 27
  • 39
  • can anyone explain the concept of special case? for example, I want to compare user input, which are unicode strings, against a stored set of unicode strings and find matches, after lowercasing both sets. would u need tolowerspecial() is this case? – Luke W Apr 01 '17 at 17:15
  • Unfortunately, I'm not sure. You could try asking a separate question about that and linking it here? – Ryan Endacott Apr 03 '17 at 20:19
  • "...For Turkish some letters are not handled correctly. Uppercase 'İ' should correspond to lowercase 'i', uppercase 'I' should correspond to lowercase 'ı' and lowercase 'i' should correspond to uppercase 'İ' ..." from https://stackoverflow.com/q/50135094/3166697 – Dima Kozhevin Aug 16 '20 at 19:58