lstIQCodes = IQcodeString.replaceAll(' ','').split(','); 

I have tried the above code and able to replace spaces and now I want to replace newline as well.


If you just want to get rid of all the white space, you can just delete it:

lstIQCodes = IQcodeString.deleteWhitespace().split(','); 

ReplaceAll actually takes a regular expression for the first parameter, so the following is also valid:

lstIQCodes = IQcodeString.replaceAll('\\s+','').split(','); 

\\s refers to all white space, including tabs, new lines, carriage returns, and spaces.

Or, if you really just wanted spaces and newlines:

lstIQCodes = IQcodeString.replaceAll('[\\n ]+','').split(','); 
  • 1
    Exactly what I would have recommended as well. Using split is not really appropriate. – Adrian Larson Jun 26 '18 at 6:32

You can follow the same approach to remove the new lines as well

for(String lines : IQcodeString.replaceAll(' ','').split(','))
     system.debug(lines.replaceAll('\n','').replaceAll('\r','') );

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.