Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swedish characters in Go Lang

Tags:

string

go

The following function does not work with Swedish characters, i.e å/Å/ä/Ä/ö/Ö.

func StartsWithUppercase(s string) bool {
    return (string(s[0]) == strings.ToUpper(string(s[0])))
}

How do I proceed to check if a string starts with upper case Swedish character?

w := "åÅäÄöÖ"
for i := 0; i < len(w); i++ {
    fmt.Println(i, w[i])
}

Results in:

 1. 195 
 2. 165
 3. 195
 4. 133
 5. 195 
 6. 164
 7. 195
 8. 132
 9. 195
 10. 182 
 11. 195
 12. 150
like image 769
Helios Avatar asked Feb 25 '15 12:02

Helios


People also ask

How can I type Swedish characters without a Swedish keyboard?

This page allows you to easily type Swedish characters without a Swedish keyboard. You can edit your text in the box and then copy it to your document, e-mail message, etc. Still copying & pasting your Swedish characters? Get the TypeIt App for PC to type Swedish characters directly into your documents and messages.

What is Swedish language?

Swedish ( svenska [²svɛnːska] (listen)) is a North Germanic language spoken natively by 10 million people, predominantly in Sweden (as the sole official language), and in parts of Finland, where it has equal legal standing with Finnish. It is largely mutually intelligible with Norwegian and to some extent with Danish,...

What is the grammar like in Swedish?

Grammar. As Swedish is a Germanic language, the syntax shows similarities to both English and German. Like English, Swedish has a subject–verb–object basic word order, but like German it utilizes verb-second word order in main clauses, for instance after adverbs and adverbial phrases, and dependent clauses.

What are the letters in Swedish alphabet?

Swedish has all the letters of the English alphabet plus three extra ones, they are the letters Å, Ä, and Ö. These three letters are considered as separate letters and not letters with diacritical marks. They come in alphabetical order after the letter Z. There are multiple ways to type these letters Å, Ä, and Ö into a program.


1 Answers

Indexing a string indexes its bytes not its runes (a rune is a unicode codepoint).

What you want to do is check the first character (rune) of the string, not its first byte in its UTF-8 encoded form. And for this there is support in the standard library: unicode.IsUpper().

To get the first rune, you can convert the string to a slice of runes, and take the first element (at index 0).

ins := []string{
    "å/Å/ä/Ä/ö/Ö",
    "Å/ä/Ä/ö/Ö"}

for _, s := range ins {
    fmt.Println(s, unicode.IsUpper([]rune(s)[0]))
}

Output:

å/Å/ä/Ä/ö/Ö false
Å/ä/Ä/ö/Ö true
like image 197
icza Avatar answered Oct 18 '22 02:10

icza