Wednesday, December 26, 2018

How to remove all special characters from String in Apex

This post explains how to remove special characters from the string in Apex
Example:
String strText = 'Welcome - to! % $sale&sforce \\ /code # crack %';
strText = strText.replaceAll('[^a-zA-Z0-9\\s+]', '');
System.debug('strText ======> '+strText);

Result
Happy Learning!!!

2 comments:

  1. How to handle this for all kinds of regional languages as well ? I mean if I want to remove any special characters in any String containing Regional Languages.

    ReplyDelete
  2. Nice one! would you suggest this one to "clean" phone fields for example?

    ReplyDelete