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
+6.9k Default cipher that OpenSSL used to encrypt a PEM file
+12.5k Golang : HTTP response JSON encoded data
+4.7k Golang : How to pass data between controllers with JSON Web Token
+11.6k Android Studio : Create custom icons for your application example
+8k Setting $GOPATH environment variable for Unix/Linux and Windows
+18k Golang : Login and logout a user after password verification and redirect example
+12.3k Golang : How to display image file or expose CSS, JS files from localhost?
+5.2k Golang : Issue HTTP commands to server and port example
+7.8k Golang : Example of how to detect which type of script a word belongs to
+31.2k Golang : Calculate percentage change of two values
+9.6k Golang : Read file with ioutil