To get a substring from a specific index to the end of the string in Go, you can use string slicing. If str
is the string, and we need the substring after an index i
, then the syntax of the expression is str[i:]
. This expression returns the substring from specified index till the ending of the string.
In this example,
str
with a value of "Hello World"
.str[5:]
to get the substring from index 5
to the end of the string.package main
import "fmt"
func main() {
str := "Hello World"
substring := str[5:]
fmt.Println("Substring from index 5 to end:", substring)
}
Substring from index 5 to end: World
In this example,
text
with a value of "Go Programming"
.text[3:]
to get the substring from index 3
to the end of the string.package main
import "fmt"
func main() {
text := "Go Programming"
substring := text[3:]
fmt.Println("Substring from index 3 to end:", substring)
}
Substring from index 3 to end: Programming
In this tutorial, we learned how to get a substring from a specific index to the end of the string in Go language, using string slicing, with well detailed examples.