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
Edward HemsworthEdward Hemsworth 

how to remove special character like \\ in apex?

Hey I am a beginner I don't know how to solve this please help me
For this array:-
[{\\"stage\\":\\"10 - Application\\",\\"oppId\\":\\"2dwd23d231d2w\\",\\"message\\":\\"Opportunity status created.\\"},{\\"stage\\":closedwon,\\"oppId\\":\\"2dwd23d231d2w\\",\\"message\\":\\"Opportunity not found or permission denied.\\"}]
Best Answer chosen by Edward Hemsworth
Akshay Dhiman 63Akshay Dhiman 63
Hi Edward,

Below code works fine please check this out:-

String source = '"[{\\"stage\\":\\"10 - Application\\",\\"oppId\\":\\"2dwd23d231d2w\\",\\"message\\":\\"Opportunity status created.\\"},{\\"stage\\":closedwon,\\"oppId\\":\\"2dwd23d231d2w\\",\\"message\\":\\"Opportunity not found or permission denied.\\"}]"';
String decodedJson = (String)JSON.deserialize(source, String.class);
System.debug('DecodedJson>>>'+decodedJson);

Hope this explanation will resolve your query. Mark it as the best answer if you find it helpful.
Thanks
Akshay

All Answers

VinayVinay (Salesforce Developers) 
Hi Edward,

Below is the sample snippet.
String strText = 'Welcome - to! % $sale&sforce \\ /code # crack %';
strText = strText.replaceAll('[^a-zA-Z0-9\\s+]', '');
System.debug('strText ======> '+strText);

References:
https://salesforce.stackexchange.com/questions/169773/apex-regex-to-remove-the-special-character/169774#:~:text=2%20Answers&text=replaceAll('%5C%5CD',digit%20from%20the%20phone%20number.
https://www.biswajeetsamal.com/blog/remove-all-special-characters-from-string-in-apex/

Hope this helps...

Thanks,
Akshay Dhiman 63Akshay Dhiman 63
Hi Edward,

Below code works fine please check this out:-

String source = '"[{\\"stage\\":\\"10 - Application\\",\\"oppId\\":\\"2dwd23d231d2w\\",\\"message\\":\\"Opportunity status created.\\"},{\\"stage\\":closedwon,\\"oppId\\":\\"2dwd23d231d2w\\",\\"message\\":\\"Opportunity not found or permission denied.\\"}]"';
String decodedJson = (String)JSON.deserialize(source, String.class);
System.debug('DecodedJson>>>'+decodedJson);

Hope this explanation will resolve your query. Mark it as the best answer if you find it helpful.
Thanks
Akshay
This was selected as the best answer
Edward HemsworthEdward Hemsworth
Thankyou Both! I give 2nd as Best because I implement this.
Malika Pathak 9Malika Pathak 9
Hi,
There are different ways to remove special characters you can remove all special characters and also remove particular characters.
   
   To remove all characters use: -
   String name = '{B]@i%s*w:?,-a$j#e)e(t! S^a.m}a]l[';
         name = name.replaceAll('[^a-zA-Z0-9\\s+]', '');
     system.debug(name);

   To relace particular character:-
     String str='Rohit & Mohit';
     String str2=str.replace('&','and');
     System.debug(str2);