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
+32.9k Golang : Call a function after some delay(time.Sleep and Tick)
+8.6k Golang : Qt Yes No and Quit message box example
+5.8k Unix/Linux : How to get own IP address ?
+17.7k Golang : Padding data for encryption and un-padding data for decryption
+10k Golang : Sieve of Eratosthenes algorithm
+4.1k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+8.9k Golang : Turn string or text file into slice example
+9.6k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+7.2k Swift : Convert (cast) String to Float
+6.8k SSL : How to check if current certificate is sha1 or sha2 from command line
+5.6k Golang : Experimental emojis or emoticons icons programming language
+5.8k Golang : Embedded or data bundling example