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
+46k Golang : Marshal and unmarshal json.RawMessage struct example
+21.1k Curl usage examples with Golang
+5.7k Facebook : How to force facebook to scrape latest URL link data?
+6.7k Golang : How to setup a disk space used monitoring service with Telegram bot
+24.9k Golang : Storing cookies in http.CookieJar example
+5.8k PHP : Get client IP address
+14.2k Golang : How to determine if user agent is a mobile device example
+6.2k Golang : Handling image beyond OpenCV video capture boundary
+6.3k Golang : Totalize or add-up an array or slice example
+14.4k Golang : Execute function at intervals or after some delay
+6.5k Golang : Experimental emojis or emoticons icons programming language