I need to check a string using apex whether it is in the following format:

  1. Anything-Insert-Anything-True
  2. Anything-Update-Anything-True
  3. Anything-Delete-Anything-False

Anything(it can be any string).
The last one may be true or false.

How to check it using apex? need help

Boolean isMatched =
System.debug('Result : '+isMatched);

//goes here }
  • is shifted to correct place
  • Thanks a lot for the response.it is quite close. Just one thing, i can have insert or update or delete.For that what i need to do. – LoveSalesforce Mar 11 '14 at 9:15
  • Hey, it's another one. I just answered for what you have asked ;) Try this Pattern.matches('(.-*(Insert|Update|Delete)-.*-)(True|False)','abc-Insert-def-True'); – highfive Mar 11 '14 at 9:18
  • Have u tried the above? its returning false when I am passing this value: abc-Insert-def-True – LoveSalesforce Mar 11 '14 at 9:35
  • Check your matcher. It's having '-' at the end which will not match with the pattern. The only difference of my two answers is second one accepting Insert or Update or Delete – highfive Mar 11 '14 at 9:38
  • When i run this in console-> Boolean isMatched = Pattern.matches('(.-Insert-.-)(True|False)','abc-Insert-def-True'); It returns false. please check System.debug('Result : '+isMatched); – LoveSalesforce Mar 11 '14 at 9:43

You can use the apex string methods as following:

string s = 'Anything-Insert-Anything-True';

if(s.contains('-Insert-') && s.endsWith('-True')){
//repeat for your other possibilities
  • No, something more generic I am looking for. – LoveSalesforce Mar 11 '14 at 8:40
  • Hmm, could you then reflect that and any other conditions you have in your question by an update ? Else it's pretty hard for anyone to answer you an answer you're really looking for. – Samuel De Rycke Mar 11 '14 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.