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.4k Golang : Individual and total number of words counter example
+8.7k Golang : Check if user agent is a robot or crawler example
+5.2k Golang : How to solve "too many .rsrc sections" error?
+18k Mac OSX : Homebrew and Golang
+12.1k Swift : Convert (cast) Int to String ?
+7.1k Golang : Auto-generate reply email with text/template package
+22.9k Golang : Call function from another package
+26.8k Golang : Connect to database (MySQL/MariaDB) server
+7.3k Useful methods to access blocked websites
+12.1k Golang : error parsing regexp: invalid or unsupported Perl syntax
+8.9k Golang : Use regular expression to get all upper case or lower case characters example
+9.9k Android Studio : Create custom icons for your application example