Go Tidbit: Ellipsize Strings Without Breaking Unicode
Strings in Go support Unicode. If you come from a programming language like C, you may think of strings as an array of (byte-sized) characters.
In Go, you can convert a string to a byte slice, and access/manipulate each byte. But if you want to truncate or ellipsize the string to a specific length, you have to think of a string like a slice of runes.
Take this string for example: "বাংলাদেশ".
If you want to ellipsize it to a length of n characters, you may run a code like this:
| |
Huh! Something needs to be fixed. What is the sane and desired output is:
| |
To correctly truncate a string with Unicode characters, convert the string to a slice of runes first:
| |
This post is 15th of my #100DaysToOffload challenge. Want to get involved? Find out more at 100daystooffload.com.
comments powered by Disqus