How do you remove the first character of a 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 strings 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

Tags:

String

Go