How to search a string in a given string

You can use the built-in service called “indexOf” as well. This service starts the search from position 0 of the input string, unless you give it a starting position. It will only return the position of the first occurrence of the searchString “local”.

Documentation on built-in services is here.

So do the following if you have need to track how many times “local” is present -

  1. Calculate the length of the string “local”
  2. Since case does not matter to you, use “toLower” build-in service to get the lowercase equivalents of both the input and search strings
  3. Call indexOf and pass the input string, searchString, position (first time it will be 0)
  4. Note down the current position of searchString (i.e., “local” in your case), returned by indexOf
  5. Add the position and length values together, to calculate a new starting point (not 0th position) for the search
  6. Pass new starting position to indexOf, to get the next occurrence (this will be done using “Repeat”)
  7. Keep a track of all the positions separately (concatenate)

Note that position will be one value less than length, numerically, so calculate the right starting point in step 5. With “encoding” values, you can use this for other languages such as Japanese, Chinese as well.

I hope my algorithm is not confusing :slight_smile:

KM

1 Like