How to search a string in a given string

How to search a string in a given string. Is there any inbuilt service in webmethod integration server…??

For example:

I have a string input: I am a developer for local tech.
and I have to search in the above string whether that contains ‘local’ or not.
The search should be like ignore case.

please suggest on same.
Thanks in advance

Use a branch with a switch value
Then in the label attribute of the embedded step, use /local/
as a regex

I often use something similar to filter for all good https responses with /^2/

Screenshot 2021-04-28 at 08.50.19

regards,
John
Product Manager
Integration & Microservice runtime

3 Likes

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

This variation will ignore case and be “true” if the substring “local” appears anywhere within the variable value: /(?i)local/

2 Likes

Rob, which one provides better performance?
[toLower + indexOf] or the RegEx option?

KM

I don’t know. I’ve never profiled these 2 approaches. I doubt the difference between them will matter in a significant way, depending upon what you’re doing. Don’t optimize prematurely – measure/profile to see if for the overall flow it is “fast enough.” If it is, you’re done. If not, then profile to see where the time is being spent and optimize that if possible. More often than not, the things that get optimized based upon opinion that it will be faster turn out to make no difference at all.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.