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
+7.6k Golang : Print how to use flag for your application example
+9.8k Golang : Change date format to yyyy-mm-dd
+5.8k Golang : Ways to recover memory during run time.
+4.7k CodeIgniter : form input set_value cause " to become & quot
+16.5k Android Studio : AlertDialog and EditText to get user string input example
+4.3k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+8.4k Golang : How to use if, eq and print properly in html template
+6k Golang : Reverse a string with unicode
+9.5k Golang : Display list of countries and ISO codes
+3.4k PHP : Extract part of a string starting from the middle
+17.6k Golang : Count number of digits from given integer value
+8.5k Golang : Interfacing with PayPal's IPN(Instant Payment Notification) example