[英]How do you remove the first character of a string?
What is the suggested method to remove the first character of a string?删除字符串第一个字符的建议方法是什么?
I've looked through the documentation for string methods but I don't see anything that works like javascript's String.slice() .我查看了字符串方法的文档,但没有看到任何像 javascript 的String.slice()那样工作的东西。
Assuming that the question uses "character" to refer to what Go calls a rune , then use utf8.DecodeRuneInString to get the size of the first rune and then slice:假设问题使用“字符”来指代 Go 所称的rune ,然后使用utf8.DecodeRuneInString获取第一个 rune 的大小,然后切片:
func trimFirstRune(s string) string {
_, i := utf8.DecodeRuneInString(s)
return s[i:]
}
As peterSO demonstrates in the playground example linked from his comment, range on a string can also be used to find where the first rune ends:正如 peterSO 在从他的评论链接的操场示例中演示的那样,字符串上的范围也可用于查找第一个符文的结束位置:
func trimFirstRune(s string) string {
for i := range s {
if i > 0 {
// The value i is the index in s of the second
// rune. Slice to remove the first rune.
return s[i:]
}
}
// There are 0 or 1 runes in the string.
return ""
}
In Go, character string
s are UTF-8 encoded Unicode code points.在 Go 中,
string
是 UTF-8 编码的 Unicode 代码点。 UTF-8 is a variable-length encoding. UTF-8 是一种可变长度编码。
The Go Programming Language Specification
Go 编程语言规范
For statements with range clause
对于带有范围子句的语句
For a string value, the "range" clause iterates over the Unicode code points in the string starting at byte index 0. On successive iterations, the index value will be the index of the first byte of successive UTF-8-encoded code points in the string, and the second value, of type rune, will be the value of the corresponding code point.
对于字符串值,“范围”子句从字节索引 0 开始迭代字符串中的 Unicode 代码点。在连续迭代中,索引值将是连续 UTF-8 编码代码点的第一个字节的索引字符串和符文类型的第二个值将是相应代码点的值。 If the iteration encounters an invalid UTF-8 sequence, the second value will be 0xFFFD, the Unicode replacement character, and the next iteration will advance a single byte in the string.
如果迭代遇到无效的 UTF-8 序列,则第二个值将是 0xFFFD,即 Unicode 替换字符,下一次迭代将在字符串中前进一个字节。
For example,例如,
package main
import "fmt"
func trimLeftChar(s string) string {
for i := range s {
if i > 0 {
return s[i:]
}
}
return s[:0]
}
func main() {
fmt.Printf("%q\n", "Hello, 世界")
fmt.Printf("%q\n", trimLeftChar(""))
fmt.Printf("%q\n", trimLeftChar("H"))
fmt.Printf("%q\n", trimLeftChar("世"))
fmt.Printf("%q\n", trimLeftChar("Hello"))
fmt.Printf("%q\n", trimLeftChar("世界"))
}
Playground: https://play.golang.org/p/t93M8keTQP_I游乐场: https : //play.golang.org/p/t93M8keTQP_I
Output:输出:
"Hello, 世界"
""
""
""
"ello"
"界"
Or, for a more general function,或者,对于更一般的功能,
package main
import "fmt"
func trimLeftChars(s string, n int) string {
m := 0
for i := range s {
if m >= n {
return s[i:]
}
m++
}
return s[:0]
}
func main() {
fmt.Printf("%q\n", trimLeftChars("", 1))
fmt.Printf("%q\n", trimLeftChars("H", 1))
fmt.Printf("%q\n", trimLeftChars("世", 1))
fmt.Printf("%q\n", trimLeftChars("Hello", 1))
fmt.Printf("%q\n", trimLeftChars("世界", 1))
fmt.Println()
fmt.Printf("%q\n", "Hello, 世界")
fmt.Printf("%q\n", trimLeftChars("Hello, 世界", 0))
fmt.Printf("%q\n", trimLeftChars("Hello, 世界", 1))
fmt.Printf("%q\n", trimLeftChars("Hello, 世界", 7))
fmt.Printf("%q\n", trimLeftChars("Hello, 世界", 8))
fmt.Printf("%q\n", trimLeftChars("Hello, 世界", 9))
fmt.Printf("%q\n", trimLeftChars("Hello, 世界", 10))
}
Playground: https://play.golang.org/p/ECAHl2FqdhR游乐场: https : //play.golang.org/p/ECAHl2FqdhR
Output:输出:
""
""
""
"ello"
"界"
"Hello, 世界"
"Hello, 世界"
"ello, 世界"
"世界"
"界"
""
""
References:参考资料:
The Go Programming Language Specification Go 编程语言规范
This works for me:这对我有用:
package main
import "fmt"
func main() {
input := "abcd"
fmt.Println(input[1:])
}
Output is:输出是:
bcd
Code on Go Playground: https://play.golang.org/p/iTv7RpML3LO Go Playground 上的代码: https : //play.golang.org/p/iTv7RpML3LO
Another option is the utf8string
package:另一种选择是
utf8string
包:
package main
import "golang.org/x/exp/utf8string"
func main() {
s := utf8string.NewString("🧡💛💚💙💜")
t := s.Slice(1, s.RuneCount())
println(t == "💛💚💙💜")
}
https://pkg.go.dev/golang.org/x/exp/utf8string https://pkg.go.dev/golang.org/x/exp/utf8string
This is the best oneLiner solution I had come across for this useCase using strings package这是我使用字符串package 为这个用例遇到的最好的 oneLiner 解决方案
package main
import (
"fmt"
"strings"
)
func main() {
myString1 := "/abc/def"
myString2 := "Hello World"
myString3 := "HHello World"
fmt.Println(strings.TrimPrefix(myString1, "/"))
fmt.Println(strings.TrimPrefix(myString2, "/"))
fmt.Println(strings.TrimPrefix(myString3, "H"))
}
Output for the above: Output 用于上述:
abc/def
Hello World
Hello World
Go Playground link: https://go.dev/play/p/tt3GgDjHXFg?v=goprev Go 游乐场链接: https://go.dev/play/p/tt3GgDjHXFg?v=goprev
You can convert the string into an array of runes, pop the first rune and then convert the array back into a string.您可以将字符串转换为符文数组,弹出第一个符文,然后将数组转换回字符串。 Here's the one-liner:
这是单线:
str = string([]rune(str)[1:])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.