package main
import (
"fmt"
)
// Truncate string.
func TruncateString(str string, length int) string {
if length <= 0 {
return ""
}
// This code cannot support Japanese
// orgLen := len(str)
// if orgLen <= length {
// return str
// }
// return str[:length]
// Support Japanese
// Ref: Range loops https://blog.golang.org/strings
truncated := ""
count := 0
for _, char := range str {
truncated += string(char)
count++
if count >= length {
break
}
}
return truncated
}
// Main
func main() {
dataList := [][]interface{} {
{"drink", 3, "dri"},
{"drink", 6, "drink"},
{"drink", 0, ""},
{"drink", -1, ""},
{"drink", 100, "drink"},
{"pub", 100, "pub"},
{"こんにちは", 3, "こんに"},
}
for _, dl := range dataList {
r := TruncateString(dl[0].(string), dl[1].(int))
if r != dl[2].(string) {
fmt.Printf("ERROR: got=%s, want=%s", r, dl[2].(string))
}
}
}
https://play.golang.org/p/QHFLlXr8v8i
https://play.golang.org/p/iakC8xxTlFI
Reference
https://play.golang.org/p/EzvhWMljku
The below code raises an error of slice.
package main
import (
"fmt"
)
func main() {
s := "drink"
r := s[:20] // error
fmt.Println(r)
}
Top comments (6)
if you test your code, there's a bug, your will get
length + 1
substring notlength
I modified it to
Thank you for your feedback. I cannot notice my mistake. Could you give me any test cases?
Hi Takahiro. Shiming isn't exactly right. I don't see any bugs with your code vs his. But he's still half right, I'll explain why.
While your code works, in general it's not good practice to increment your counter before the end of the loop. It would be very easy for someone to make a mistake modifying this code.
Better would be this:
But best is exactly the code @huahuayu wrote because he's using the built-in
for
loop counter. He doesn't even need to worry about initializing and incrementing the counter because thefor
loop does all the hard work for him.In general, the less code the better.
Thank you for your explaining. I have made sense. 😆
Thanks! I didn't know
utf8.RuneCountInString
method. I have to learn more, haha😅