lstIQCodes = IQcodeString.replaceAll(' ','').split(',');
I have tried the above code and able to replace spaces and now I want to replace newline as well.
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this community 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(',');
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','') );