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().
The idea is to use the deleteCharAt() method of StringBuilder class to remove first and the last character of a string. The deleteCharAt() method accepts a parameter as an index of the character you want to remove.
To remove the first character of a string, we can use the char *str = str + 1 in C. it means the string starts from the index position 1. Similarly, we can also use the memmove() function in C like this.
To remove the first 2 characters from a string, use the slice method, passing it 2 as a parameter, e.g. str. slice(2) . The slice method returns a new string containing the specified portion of the original string.
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:]
}
playground example
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 ""
}
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
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
Unicode UTF-8 FAQ
The Unicode Consortium
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With