Yes there is, check the strings package.
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.ToLower("Gopher"))
}
Answer from AurA on Stack Overflowstrings: optimize ToLower and ToUpper
How to create lower-case unicode strings and also map similar looking strings to the same string in a security-sensitive setting?
word count - How to convert strings to lower case in GO? - Stack Overflow
strings,bytes: ToLower(), ToUpper() by using bitwise operator
Yes there is, check the strings package.
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.ToLower("Gopher"))
}
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"
I have an Sqlite3 database and and need to enforce unique case-insensitive strings in an application, but at the same time maintain original case for user display purposes. Since Sqlite's collation extensions are generally too limited, I have decided to store an additional down-folded string or key in the database.
For case folding, I've found x/text/collate and strings.ToLower. There is alsostrings.ToLowerSpecial but I don't understand what it's doing. Moreover, I'd like to have strings in some canonical lower case but also equally looking strings mapped to the same lower case string. Similar to preventing URL unicode spoofing, I'd like to prevent end-users from spoofing these identifiers by using similar looking glyphs.
Could someone point me in the right direction, give some advice for a Go standard library or for a 3rd party package? Perhaps I misremember but I could swear I've seen a library for this and can't find it any longer.
Edit: I've found this interesting blog post. I guess I'm looking for a library that converts Unicode confusables to their ASCII equivalents.
Edit 2: Found one: https://github.com/mtibben/confusables I'm still looking for opinions and experiences from people about this topic and implementations.
You should convert words to lowercase when you are using them as map key
for _, word := range words {
fregs[strings.ToLower(word)] += 1
}
to remove all non-word characters you could use a regular expression:
package main
import (
"bufio"
"fmt"
"log"
"regexp"
"strings"
)
func main() {
str1 := "This is some text! I want to count each word. Is it cool?"
re, err := regexp.Compile(`[^\w]`)
if err != nil {
log.Fatal(err)
}
str1 = re.ReplaceAllString(str1, " ")
scanner := bufio.NewScanner(strings.NewReader(str1))
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
fmt.Println(strings.ToLower(scanner.Text()))
}
}