I need to remove the trailing spaces from a String. However, when I try that in Execute Anonymous window with the below test apex code, I am getting the below error message.

String testString = 'ABCD124  ';
System.debug('Trimmed string = ' + rtrim(testString,' '));

Method does not exist or incorrect signature: void rtrim(string, string)

I need to retain the leading spaces, if there are any. So, I can't use the trim() method.

I also tried without the second parameter, and it did not work either.

Can someone help?

The regular expression worked, but I have problems related to this. I getting a response from REST API with some account details and I am comparing the old and new values before updating the account. Some sample code snippet is below.

if (((aRec.D__c != null && aRec.D__c.replaceAll(regExp,'') != (aUpdRec.D__c != null && aUpdRec.D__c.replaceAll(regExp, '')) ||
((aRec.P__c != null && aRec.P__c.replaceAll(regExp,'') != (aUpdRec.P__c != null && aUpdRec.P__c.replaceAll(regExp, '')))

However, the value of D__c could be null in some cases and it is throwing the below error message.

Logical Operator can only be applied to boolean

It appears that the || operation is causing the issue when the values are null. When aRec.D__c is null and aUpdRec.D__c is null, we are comparing null != null and I am not sure the result of this. I guess it is not a boolean and hence getting the error message.

Any ideas to fix this?


I was able to fix it, by adding the statements before the if condition:

if (aRec.D__c != null)
   aRec.D__c = aRec.D__c.replaceAll(regExp,'');


I have couple of fields on the accounts (D__c, P__c and some more). I make a callout to a third party webservice and get the new values for these fields on the account. I need to trim the trailing spaces on the fields before comparing. In short, I am trying to evaluate the below expression.

if ((aRec.D__c == null && aUpdRec.D__c != null) || 
    (aRec.D__c != null && aUpdRec.D__c == null) || 
    (aRec.D__c != null && aUpdRec.D__c != null &&  
     aRec.D__c.replaceAll(regExp,'') !=  
     aUpdRec.D__c.replaceAll(regExp,'')) || ==> repeat for other fields like P__c.

When I use the above expression, I am getting the message "Comparison arguments must be compatible types: NULL, Boolean.

1 Answer 1


You could a regular expression to remove spaces with String.replaceAll(regExp, replacement)

String regExp = '\\s+$';
String test = '  Hello   World     ';
System.debug('Output: ' + test.replaceAll(regExp, '') + '-- End'); // Output:   Hello   World-- End

You have a problem with parentheses.
You wrote: aRec.D__c != null && aRec.D__c.replaceAll(regExp,'') != (aUpdRec.D__c != null && aUpdRec.D__c.replaceAll(regExp, '')).
I'm going to add parentheses to explain what happens: (aRec.D__c != null) && (aRec.D__c.replaceAll(regExp,'') != (aUpdRec.D__c != null && aUpdRec.D__c.replaceAll(regExp, ''))).
The problem is here: (aRec.D__c.replaceAll(regExp,'') != (aUpdRec.D__c != null && aUpdRec.D__c.replaceAll(regExp, ''))). The left operand is a String, the second one is an expression that evaluates to a boolean. That's why you got "Logical Operator can only be applied to boolean"

Try this:

if ( (aRec.D__c?.replaceAll(regExp,'') != aUpdRec.D__c?.replaceAll(regExp, '')) ||
  (aRec.P__c?.replaceAll(regExp,'') != aUpdRec.P__c?.replaceAll(regExp, '')) )

Update: Using the ?. operator the if-blcok will be executed whether aRec.D__c or aRec.P__c will differ from their UpdRec relatives. I.E. aRec.D__c is null and aUpdRec.D__c is not and viceversa, or their values aren't equal.

Please take a look at XY Problem. If you could state what you would like to achieve, we could provide more useful answers.

  • Thanks, it worked. However, I have another problem related to this code. My sample code is below. Will update in original post Jan 7, 2021 at 16:34
  • @BalajiPooruli I've update the answer. There was a problem with parentheses in your if
    – RubenDG
    Jan 7, 2021 at 17:04
  • @RubanDG, Thanks for your answer. The if statements that you have may or may not work as there are chances where either one of them could be null. The solution I have given in the original post seems to be working as expected. Jan 7, 2021 at 17:41
  • @BalajiPooruli since those statements are in AND, if any resolves to false, the following will not be evaluated, therefore there will not be a NullPointerException, because it will evaluate aRec.D__c.replaceAll(regExp,'') != aUpdRec.D__c.replaceAll(regExp, '') only if both aRec.D__c and aUpdRec.D__c are not null. The same goes with aRec.P__c.replaceAll(regExp,'') != aUpdRec.P__c.replaceAll(regExp, '')
    – RubenDG
    Jan 7, 2021 at 18:36
  • There will not be a NullPointerException, but if aRec.D__c is not null and aUpdRec.D__c is null, the whole expression will not be evaluated and hence we will be missing that update. Let me know if I am missing something Jan 7, 2021 at 19:56

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.