function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

rtrim not working

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?
Satya Prakash ChoudharySatya Prakash Choudhary
Please try below code:

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

trim() removes the leading spaces also, which is something I don't want to do.

I used the below code and it worked perfectly.

String regExp = '\\s+$';
System.debug('Trimmed String = ' + testString.replaceAll(regExp, '');