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
+22.4k Golang : Check if element exist in map
+11.1k Golang : convert(cast) string to integer value
+5.9k Golang : Pat multiplexer routing example
+4.4k Unix/Linux : How to archive and compress entire directory ?
+6k Golang : Get Alexa ranking data example
+7.1k Golang : Auto-generate reply email with text/template package
+3.6k Javascript : Access JSON data example
+4.8k Golang : Use NLP to get sentences for each paragraph example
+31.3k Golang : Smarter Error Handling with strings.Contains()
+3.9k MariaDB/MySQL : Form select statement or search query with Chinese characters
+10.7k Golang : md5 hash of a string
+9.5k Golang : Create matrix with Gonum Matrix package example