How are you deciding the substring? If you're doing something like:
searchStr := "a string"
i := strings.Index(str, searchStr)
if i < 0 {
// string wasn't found; handle appropriately
}
substr := str[i:i+len(searchStr)]
This is always safe for any searchStr.
If you want to do something like extract a substring containing the
first 20 characters of another string, then you have to worry about
UTF-8. You could use utf8.String or convert the string to its code
points with a conversion to []int.
- Evan
Sure, I understand the problem, but to get better advice you need to
give more details. If we consider a constant string, the solution is
obvious because the string is known ahead of time. You probably want a
more general solution than that.
What substring do you want? How will you find it?
- Evan
Just one more question: what's the complexity of a casting of a string to []rune ? O(n) ?