[go-nuts] remove last index of character from string and also want use strings.LastIndex(s, sep string)
var str = "a/b/c/d/c" // I want remove last chracter from str var strRunes = []rune(str) var newStrRunes = strRunes[0 : len(strRunes)-1] // then I want get last index of chracters `/c`, I need convert to string back!??? strings.LastIndex(string(newStrRunes), "/c") // Does there have a method that LastIndex(rs []rune, s string) Thanks for your help! -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] remove last index of character from string and also want use strings.LastIndex(s, sep string)
Thanks for your reply. the `str[0:len(str)-1]` not work for NO ascii chracters. https://play.golang.org/p/7i6-3Zcy36 On Friday, May 26, 2017 at 11:06:10 PM UTC+8, messju mohr wrote: > > Hi, > > On Fri, May 26, 2017 at 07:10:43AM -0700, long...@gmail.com > wrote: > >var str = "a/b/c/d/c" > >// I want remove last chracter from str > >var strRunes = []rune(str) > >var newStrRunes = strRunes[0 : len(strRunes)-1] > >// then I want get last index of chracters `/c`, I need > convert to string back!??? > >strings.LastIndex(string(newStrRunes), "/c") > >// Does there have a method that LastIndex(rs []rune, s > string) > >Thanks for your help! > > You don't have to convert to a slice of runes: > > var str = "a/b/c/d/c" > var newStr = str[0:len(str)-1] > fmt.Println(strings.LastIndex(newStr, "/c")) > > Or you have to convert your slice back to a string: > > var str = "a/b/c/d/c" > var strRunes = []rune(str) > var newStrRunes = strRunes[0 : len(strRunes)-1] > fmt.Println(strings.LastIndex(string(newStrRunes), "/c")) > > HTH > messju > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Re: remove last index of character from string and also want use strings.LastIndex(s, sep string)
I find the method like: package main import ( "fmt" "strings" "unicode/utf8" ) func main() { var str = "a/b/汉字/d/汉字" var _, size = utf8.DecodeLastRuneInString(str) // Does `str[0:len(str)-size]` have memory copy? var i = strings.LastIndex(str[0:len(str)-size], "/汉字") fmt.Println(i) } But I don't know if str[0:len(str)-size] if copy the string? If the code will copy the string, I will covert the string to []byte first. Because I need several times check for the str. Where I can find some article about that(str copy)? On Friday, May 26, 2017 at 10:45:09 PM UTC+8, long...@gmail.com wrote: > > var str = "a/b/c/d/c" > // I want remove last chracter from str > var strRunes = []rune(str) > var newStrRunes = strRunes[0 : len(strRunes)-1] > // then I want get last index of chracters `/c`, I need > convert to string back!??? > strings.LastIndex(string(newStrRunes), "/c") > // Does there have a method that LastIndex(rs []rune, s > string) > > Thanks for your help! > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.