Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search for substr from position #42924

Closed
lijh8 opened this issue Dec 1, 2020 · 5 comments
Closed

search for substr from position #42924

lijh8 opened this issue Dec 1, 2020 · 5 comments

Comments

@lijh8
Copy link

lijh8 commented Dec 1, 2020

Can I search for a substr starting from a specific position:

    func Index(s, substr string, pos int) int

like this function in C++:

    // search from position 5
    string const s = "This is a string";
    n = s.find("is", 5);
@daduam
Copy link

daduam commented Dec 1, 2020

The string can be treated as a slice

package main

import "strings"

func main() {
    s := "This is a string"
    n := strings.Index(s[5:], "is")
}

@lijh8
Copy link
Author

lijh8 commented Dec 1, 2020

Thanks

@lijh8 lijh8 closed this as completed Dec 1, 2020
@lijh8
Copy link
Author

lijh8 commented Dec 1, 2020

n2 := strings.Index(s[n1:], "is")
so n1+n2 will be the position index of substr in the original string, right

@daduam
Copy link

daduam commented Dec 1, 2020

yes. I forgot to mention that the slice is sort of a new string so indexing begins at 0 again

@davecheney
Copy link
Contributor

Thank you for raising this issue. Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For asking questions, see:

@golang golang locked and limited conversation to collaborators Dec 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants