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
+17.2k How to enable MariaDB/MySQL logs ?
+5.9k Golang : Detect face in uploaded photo like GPlus
+5.5k Golang : Denco multiplexer example
+19.3k Golang : How to get own program name during runtime ?
+9k Golang : Terminate-stay-resident or daemonize your program?
+16.8k Golang : Check if IP address is version 4 or 6
+22.2k Golang : simulate tail -f or read last line from log file example
+19.2k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+6.7k Golang : Validate credit card example
+11.9k Golang : How to display image file or expose CSS, JS files from localhost?
+21.6k Golang : Match strings by wildcard patterns with filepath.Match() function
+21.9k Golang : How to read JPG(JPEG), GIF and PNG files ?