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
+27.5k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+8k Golang : Emulate NumPy way of creating matrix example
+8.4k Golang : On lambda, anonymous, inline functions and function literals
+9.7k Golang : Channels and buffered channels examples
+20.2k Golang : Secure(TLS) connection between server and client
+38.8k Golang : How to read CSV file
+8.9k Golang : How to check if a string with spaces in between is numeric?
+32.7k Delete a directory in Go
+31.8k Golang : Math pow(the power of x^y) example
+8.2k Golang : How to check variable or object type during runtime?
+11.7k Golang : Sort and reverse sort a slice of runes
+16.6k Golang : Capture stdout of a child process and act according to the result