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
+4.3k Golang : Valued expressions and functions example
+14.2k Golang : How to shuffle elements in array or slice?
+17.9k Golang : Put UTF8 text on OpenCV video capture image frame
+15.5k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+5.2k Javascript : Shuffle or randomize array example
+9.1k Golang : How to get garbage collection data?
+8k Android Studio : Rating bar example
+8.7k Yum Error: no such table: packages
+15.1k nginx: [emerg] unknown directive "ssl"
+32k Golang : Convert []string to []byte examples
+11.2k Use systeminfo to find out installed Windows Hotfix(s) or updates
+13.9k Javascript : Prompt confirmation before exit