Golang : Trim everything onward after a word
Putting this simple example here for my own future reference. Basically, the problem that I'm trying to solve is to get rid of the rest from from a sentence after a word.
For example, I want to get rid of everything from "abc" onward. How to do that?
BEFORE :
s := "123123123123123456abc789123123123123"
AFTER :
s := "123123123123123456"
The solution is to use strings.LastIndex()
to get the position of abc
and then only capture the part before the position.
package main
import (
"fmt"
"strings"
)
func main() {
s := "123123123123123456abc789123123123123"
position := strings.LastIndex(s, "abc")
fmt.Println(s[:position])
}
Hope this help!
Happy computing!
See also : Golang : package is not in GOROOT during compilation
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+13.9k Golang : Compress and decompress file with compress/flate example
+9.7k Golang : Turn string or text file into slice example
+5.1k JavaScript/JQuery : Redirect page examples
+15.6k Golang : How to login and logout with JWT example
+5.5k Golang : Detect words using using consecutive letters in a given string
+18.7k Golang : Delete duplicate items from a slice/array
+8.2k Your page has meta tags in the body instead of the head
+8.2k Golang : Number guessing game with user input verification example
+7.6k Golang : Generate human readable password
+13.3k Golang : Get constant name from value
+21.6k Golang : Convert string slice to struct and access with reflect example