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
+13.7k Golang : Underscore or snake_case to camel case example
+3.4k Python : Delay with time.sleep() function example
+18k Generate checksum for a file in Go
+27k Golang : Validate email address with regular expression
+13.3k Golang : Convert IPv4 address to decimal number(base 10) or integer
+6k Golang : Heap sort example
+8.1k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+13.4k CodeIgniter/PHP : Create directory if does not exist example
+20.6k Get file path of temporary file in Go
+7.3k Android Studio : Create custom icons for your application example
+10.1k Golang : Save(pipe) HTTP response into a file