在 Golang 中达到定义的最大长度之前,如何将跟随者字符连接到字符串?
How to concatenate follower characters to a string until a defined maximum length has been reached in Golang?
输入输出
abc abc___
a a___
abcdeabcde_
尝试
package main
import "fmt"
import "unicode/utf8"
func main() {
input := "abc"
if utf8.RuneCountInString(input) == 1 {
fmt.Println(input + "_____")
} else if utf8.RuneCountInString(input) == 2 {
fmt.Println(input + "____")
} else if utf8.RuneCountInString(input) == 3 {
fmt.Println(input + "___")
} else if utf8.RuneCountInString(input) == 4 {
fmt.Println(input + "__")
} else if utf8.RuneCountInString(input) == 5 {
fmt.Println(input + "_")
} else {
fmt.Println(input)
}
}
returns
abc___
讨论
虽然代码创建了预期的输出,但它看起来非常冗长和曲折。
问题
有没有简洁的方法?
您可以只在一个循环中执行 input += "_"
,但这会分配不必要的字符串。这是一个分配的版本不超过它的需要:
const limit = 6
func f(s string) string {
if len(s) >= limit {
return s
}
b := make([]byte, limit)
copy(b, s)
for i := len(s); i < limit; i++ {
b[i] = '_'
}
return string(b)
}
strings 包有一个 Repeat
函数,所以像
input += strings.Repeat("_", desiredLen - utf8.RuneCountInString(input))
会更简单。您可能应该首先检查 desiredLen
是否小于输入长度。
您也可以在没有循环和 "external" 函数调用的情况下高效地执行此操作,方法是将准备好的 "max padding" 切片(切出所需的填充并将其简单地添加到输入中):
const max = "______"
func pad(s string) string {
if i := utf8.RuneCountInString(s); i < len(max) {
s += max[i:]
}
return s
}
使用它:
fmt.Println(pad("abc"))
fmt.Println(pad("a"))
fmt.Println(pad("abcde"))
输出(在 Go Playground 上尝试):
abc___
a_____
abcde_
备注:
len(max)
是常数(因为max
是常数):Spec: Length and capacity:
The expression len(s)
is constant if s
is a string constant.
切片 string
是 efficient:
An important consequence of this slice-like design for strings is that creating a substring is very efficient. All that needs to happen is the creation of a two-word string header. Since the string is read-only, the original string and the string resulting from the slice operation can share the same array safely.
输入输出
abc abc___
a a___
abcdeabcde_
尝试
package main
import "fmt"
import "unicode/utf8"
func main() {
input := "abc"
if utf8.RuneCountInString(input) == 1 {
fmt.Println(input + "_____")
} else if utf8.RuneCountInString(input) == 2 {
fmt.Println(input + "____")
} else if utf8.RuneCountInString(input) == 3 {
fmt.Println(input + "___")
} else if utf8.RuneCountInString(input) == 4 {
fmt.Println(input + "__")
} else if utf8.RuneCountInString(input) == 5 {
fmt.Println(input + "_")
} else {
fmt.Println(input)
}
}
returns
abc___
讨论
虽然代码创建了预期的输出,但它看起来非常冗长和曲折。
问题
有没有简洁的方法?
您可以只在一个循环中执行 input += "_"
,但这会分配不必要的字符串。这是一个分配的版本不超过它的需要:
const limit = 6
func f(s string) string {
if len(s) >= limit {
return s
}
b := make([]byte, limit)
copy(b, s)
for i := len(s); i < limit; i++ {
b[i] = '_'
}
return string(b)
}
strings 包有一个 Repeat
函数,所以像
input += strings.Repeat("_", desiredLen - utf8.RuneCountInString(input))
会更简单。您可能应该首先检查 desiredLen
是否小于输入长度。
您也可以在没有循环和 "external" 函数调用的情况下高效地执行此操作,方法是将准备好的 "max padding" 切片(切出所需的填充并将其简单地添加到输入中):
const max = "______"
func pad(s string) string {
if i := utf8.RuneCountInString(s); i < len(max) {
s += max[i:]
}
return s
}
使用它:
fmt.Println(pad("abc"))
fmt.Println(pad("a"))
fmt.Println(pad("abcde"))
输出(在 Go Playground 上尝试):
abc___
a_____
abcde_
备注:
len(max)
是常数(因为max
是常数):Spec: Length and capacity:
The expression
len(s)
is constant ifs
is a string constant.
切片 string
是 efficient:
An important consequence of this slice-like design for strings is that creating a substring is very efficient. All that needs to happen is the creation of a two-word string header. Since the string is read-only, the original string and the string resulting from the slice operation can share the same array safely.