Reputation: 452
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().
Upvotes: 30
Views: 57622
Reputation: 1145
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:])
Upvotes: 3
Reputation: 69
This is the best oneLiner solution I had come across for this useCase using strings package
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:
abc/def
Hello World
Hello World
Go Playground link: https://go.dev/play/p/tt3GgDjHXFg?v=goprev
Upvotes: 2
Reputation: 1
Another option is the utf8string
package:
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
Upvotes: 3
Reputation: 120969
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:
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:
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 ""
}
Upvotes: 29
Reputation: 166598
In Go, character string
s are UTF-8 encoded Unicode code points. UTF-8 is a variable-length encoding.
The Go Programming Language Specification
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. 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.
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
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
Output:
""
""
""
"ello"
"η"
"Hello, δΈη"
"Hello, δΈη"
"ello, δΈη"
"δΈη"
"η"
""
""
References:
The Go Programming Language Specification
Upvotes: 17
Reputation: 3674
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
Upvotes: 20