It's an annoying oversight, you have to create your own function.
Something like:
func indexAt(s, sep string, n int) int {
idx := strings.Index(s[n:], sep)
if idx > -1 {
idx += n
}
return idx
}
Answer from OneOfOne on Stack OverflowIt's an annoying oversight, you have to create your own function.
Something like:
func indexAt(s, sep string, n int) int {
idx := strings.Index(s[n:], sep)
if idx > -1 {
idx += n
}
return idx
}
No, but it might be simpler to apply strings.Index on a slice of the string
strings.Index(s[1:], "go")+1
strings.Index(s[n:], "go")+n
See example (for the case where the string isn't found, see OneOfOne's answer), but, as commented by Dewy Broto, one can simply test it with a 'if' statement including a simple statement:
(also called 'if' with an initialization statement)
if i := strings.Index(s[n:], sep) + n; i >= n {
...
}
Help: Finding all occurrences of a text inside a string, between a given start and finish
proposal: strings: add multiple string search ContainsAnySubstring and IndexAnySubstring
string - How to find a character index in Golang? - Stack Overflow
Find all index of occurrences of character in a string
Hello everyone,
I'm tring to do something interesting, but I don't know how to do it the Go way. The problem I'm trying to solve is: given a long string of text like:
Fabio vel iudice vincam, sunt in culpa qui officia. Ullamco laboris nisi ut aliquid ex ea commodi consequat. At nos hinc posthac, sitientis piros Afros. A communi observantia non est recedendum. Quisque placerat facilisis egestas cillum dolore.
I need a function that helps me find all occurrences of something between two other strings of text, all of their occurrences, i.e. if I search for anything between un and (space) I got a slice of strings with a "t" at the zero position and a "i" at the first position (that because "sunt in" matches the description of anything between "un" and " " and also "communi observantia" matches, because "commUNi ")
I've just made a function but it only finds the first occurrence and I might think that this is not the Go way of doing things. Look here: http://play.golang.org/p/NuVJGfQ4GB
You can use the Index function of package strings
Playground: https://play.golang.org/p/_WaIKDWCec
package main
import "fmt"
import "strings"
func main() {
x := "chars@arefun"
i := strings.Index(x, "@")
fmt.Println("Index: ", i)
if i > -1 {
chars := x[:i]
arefun := x[i+1:]
fmt.Println(chars)
fmt.Println(arefun)
} else {
fmt.Println("Index not found")
fmt.Println(x)
}
}
If you are searching for non-ASCII characters (languages other than english) you need to use http://golang.org/x/text/search.
func SubstringIndex(str string, substr string) (int, bool) {
m := search.New(language.English, search.IgnoreCase)
start, _ := m.IndexString(str, substr)
if start == -1 {
return start, false
}
return start, true
}
index, found := SubstringIndex('Aarhus', 'ร
');
if found {
fmt.Println("match starts at", index);
}
Search the language.Tag structs here to find the language you wish to search with or use language.Und if you are not sure.