I need to perform a keyword search in a string. Similar to the below: If the Keyword is cancel, then the match should be true if the string is "Cancel the flight" and the match should be false if the string is "Cancelling the flight".

Also the keyword can be two words as well. For example: Booking code.

I tried the string methods like contains, equals etc. But they do not work.


4 Answers 4


Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

They really are the right tool for the job here though.

static Boolean keywordSearch(String input, String keywordPhrase)
    return Pattern.compile('(?i)\\b' + keywordPhrase + '\\b').matcher(input).find();

String corpus = 'The quick brown fox jumps over the lazy dog';
system.assert(keywordSearch(corpus, 'lazy dog'));
system.assert(keywordSearch(corpus, 'QUICK'));
system.assert(!keywordSearch(corpus, 'jump'));

The \b character indicates a word boundary. Wrap your phrase in that and see if it's in the input. The (?i) argument makes the whole thing case-insensitive.

  • 1
    I tried the pattern and matcher before as well. But did not know about \\b.. Thanks a lot.. this worked now! Aug 31, 2016 at 16:10
  • @SFDCLearner If this answer addressed your problem, please consider accepting it by clicking on the check mark/tick to the left of the answer, turning it green. This marks the question as resolved to your satisfaction, and awards reputation both to you and the person who answered. If you have >= 15 reputation points, you may also upvote the answer if you wish. There is no obligation to do either.
    – Adrian Larson
    Aug 31, 2016 at 16:23
  • Hi Adrian.. The above did not work if there were special characters in the keyword. I tried adding \\w to the code to detect the special characters.. but it did not work.. Could you please point me how this can be done or point me where i can check for resources related to it Sep 7, 2016 at 17:12
  • @SFDCLearner 1) Which special characters? 2) You should ask a separate question about how to handle that and link back to this one.
    – Adrian Larson
    Sep 7, 2016 at 17:13
  • the special character can be anything.. * , (, ) etc. I have created a new question but i do not know how to link it to this question Sep 7, 2016 at 17:17

well since you are concerned of the only the exact word match. I would suggest to split the search string into words such as String[] words = query.split(' ');

Then you can loop though all the words such as

integer ok=0;
for(integer i =0 ;i< words.size(); i++)
 if( words[i]=='Cancel')

return query;
return false;

You can adapt this example into a function. Note: I haven't tried out the code above so there might be some syntax errors, but this should give you a good starting point.


You can achieve this by using set as well.

String str = 'Cancel the flight';
set<String> set1 = new Set<String>();
set1.addAll(new List<String>(str.split(' ')));

but here the string is case sensitive so if you search cancel in the above string it will return false.


So you have 2 strings in Apex like:

String s1 = 'Cancel the flight';
String s2 = 'Cancelling the flight';
String keyword = 'cancel';

And you need to determine if s1 and s2 contains keyword? What about Case sensitivity? keyword has a lower case c while s1 and s2 have capitol.

This will give you what you want. Remove the toLowerCase() call if you want case sensitive match.

new Set<String>(s1.toLowerCase().split(' ')).contains(keyword.toLowerCase());
new Set<String>(s2.toLowerCase().split(' ')).contains(keyword.toLowerCase());

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .