Menu Close

Go – ToLowerSpecial() function in strings package in go

Here, We will learn about ToLowerSpecial() function in strings package in go.

The strings.ToLowerSpecial() function in Go is used to convert a given string to lowercase using a special case mapping. It is a part of the standard library in Go. It is often used in applications where case-insensitive string comparisons are required.

Function prototype:

func ToLowerSpecial(c unicode.SpecialCase, s string) string

The function takes two parameters:

  • 'c' – the case-folding mapping to use. This parameter is optional and can be set to nil to use the default case-folding mapping. The case-folding mapping defines the conversion rules for certain characters, such as the Turkish dotless “i” or the Greek letter sigma.
  • 's' – the string to convert.

The function returns a new string that is a copy of the input string with all its characters converted to lowercase.

Example:

package main

import (
    "fmt"
    "unicode"
)

func main() {
    s := "HELLO, WORLD!"
    
    fmt.Println(strings.ToLowerSpecial(unicode.TurkishCase, s))
}

Output:

hello, world!

In the above example, the ToLowerSpecial() function is used to convert the string “HELLO, WORLD!” to lowercase, using the Turkish case-folding mapping. In Turkish, the lowercase version of the letter “I” is “ı”, which is different from the lowercase version in most other languages, such as “i”. By using the Turkish case-folding mapping, the ToLowerSpecial() function correctly converts the uppercase “I” in the input string to lowercase “ı”.

Note that ToLowerSpecial() does not modify the input string, but returns a new string with the converted characters. Also, if the input string contains characters that do not have a lowercase equivalent, such as numbers or punctuation marks, those characters are left unchanged in the output string.

Conclusion

strings.ToLowerSpecial() is a useful function in Go for converting strings to lowercase, with support for different case-folding mappings.

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/pkg/

Posted in golang, strings

Leave a Reply

Your email address will not be published. Required fields are marked *