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.