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
+21.9k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+19.7k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+18.7k Golang : Read input from console line
+9.2k Facebook : Getting the friends list with PHP return JSON format
+31.2k Golang : Example for ECDSA(Elliptic Curve Digital Signature Algorithm) package functions
+10k Golang : How to check if a website is served via HTTPS
+4.6k Which content-type(MIME type) to use for JSON data
+20.9k Golang : Get password from console input without echo or masked
+17.4k Golang : Parse date string and convert to dd-mm-yyyy format
+7.1k Golang : alternative to os.Exit() function
+4.8k Golang : Calculate a pip value and distance to target profit example