Searching recursively in GOLANG

72 views
Skip to first unread message

Sharan Guhan

unread,
Mar 16, 2021, 2:06:15 AM3/16/21
to golang-nuts
Hi Experts,

Seems a trivial problem, but unable to find the right imports to do this.. Want to search for a substring in a huge string ( output of a exec.command) and get the index for every occurrence of that substring.  I used Index and LastIndex, both give first and last, but am looking for IndexNext kind of till the last occurrence....

For example : "Hello this is Golang program Golang has a variety of import utilities.I like to code in Golang"

In the above example, I want an API which will return each occurrence of Golang


Sharanya

Jan Mercl

unread,
Mar 16, 2021, 2:18:32 AM3/16/21
to Sharan Guhan, golang-nuts
The API is your keyboard ;-) Joking aside, it's easy to write a loop
that uses Index to find out the first position, slice the string to
remove the occurrence and find another until no more exists.

Sharan Guhan

unread,
Mar 16, 2021, 2:55:29 AM3/16/21
to Jan Mercl, golang-nuts
Thanks for the tip!  I just wanted to make sure, I am not missing some basic API out there.. Will code this up :-)

Sharan

jake...@gmail.com

unread,
Mar 17, 2021, 11:40:36 AM3/17/21
to golang-nuts
You could also achieve this using the regexp package. But, in this case, it would probably be overkill.
Reply all
Reply to author
Forward
0 new messages