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
crazyforcercrazyforcer 

How to differentiate " and \" in apex string variable?

Hi,

string str = '"abcde\"fgh"'; // "abcde\"fgh"

Apex will treat first and last " and \" are same. But I would like to replace \" with '#DQ#'. That is, I should get "abcde#DQ#fgh".
How can this be achieved? If this is a silly question, please forgive me. :-)

 
Amit Chaudhary 8Amit Chaudhary 8
' ' is used to pass any String like below
String name ='amit';

but if you want to pass any special char (wildcard) in string then you can use '\'
like below example

If i want to create a query like below in String
select id from account where name lile '%amit%';



Then
String strQuery = 'select id from acount where Name Like \''+acc.Name+'%\' ';

Please let us know if this will help you

Thanks,
Amit Chaudhary
crazyforcercrazyforcer
Sorry Amit. It won't help. It is not the case of a SOQL. Just finding and replacing \" inside a string. Thanks for the effort.

In my case the value to be stored in the variable is "abcde\"fgh" where first " and last " along with \" are the chracters in the string. So,

string str = '"abcde\"fgh"'; // "abcde\"fgh"

So, I should have something like:

str = str.replace('\"', '#DQ#');

But if I use like this, the output string will be #DQ#abcde#DQ#fgh#DQ#. That is, apex will treat normal double quote (") and escaped double quote (\") as same. My requirement is to differentiate them as I need to use a double quoute inside double quote.