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.1k Golang : How to make a file read only and set it to writable again?
+6.7k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+6.2k Elasticsearch : Shutdown a local node
+10.6k Golang : Create S3 bucket with official aws-sdk-go package
+6.9k Golang : Calculate how many weeks left to go in a given year
+16.5k Golang : Get input from keyboard
+13.4k Golang : Gin framework accept query string by post request example
+17.6k Golang : Convert IPv4 address to decimal number(base 10) or integer
+18.2k Golang : Padding data for encryption and un-padding data for decryption
+8.9k Golang : Temperatures conversion example
+17k Golang : Linked list example
+28.2k Golang : Detect (OS) Operating System