+ Start a Discussion
TerminusbotTerminusbot 

Apex String - Add a backslash to a string.

I am trying to insert a \ into a string. In front of the date and the end of the date I need to insert the \. Can this be done? 
 
writer.writeAttribute(null,null,'statement', 'LIST POLICIES LAST.ENTRY.DATE GT \7/26/2016\ *OUTPUT* CLIENT.CODE 31 32 33');

 
Best Answer chosen by Terminusbot
Martijn SchwarzerMartijn Schwarzer
Hi Terminusbot,

If you want to add a backslash into a string, you will need to add double backslashes. The first one will escape the second one, which will result in a single backslash in your string.

Example:
 
String myString = 'This is a string with a \\ in it';

This will result in:

This is a string with a \ in it

Hope this helps!

Best regards,
Martijn Schwärzer

Ps. If my answer helped you, please mark it as best answer. It will help others find best answer as well.

All Answers

Martijn SchwarzerMartijn Schwarzer
Hi Terminusbot,

If you want to add a backslash into a string, you will need to add double backslashes. The first one will escape the second one, which will result in a single backslash in your string.

Example:
 
String myString = 'This is a string with a \\ in it';

This will result in:

This is a string with a \ in it

Hope this helps!

Best regards,
Martijn Schwärzer

Ps. If my answer helped you, please mark it as best answer. It will help others find best answer as well.
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
You can try like below
String str= 'LIST POLICIES LAST.ENTRY.DATE GT \\7/26/2016\\ *OUTPUT* CLIENT.CODE 31 32 33';
System.debug('-------->'+str);
Output will be like below
37.0 APEX_CODE,DEBUG
Execute Anonymous: String str= 'LIST POLICIES LAST.ENTRY.DATE GT \\7/26/2016\\ *OUTPUT* CLIENT.CODE 31 32 33';
Execute Anonymous: System.debug('-------->'+str);
00:30:43.17 (17884141)|EXECUTION_STARTED
00:30:43.17 (17892456)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
00:30:43.17 (19108523)|USER_DEBUG|[2]|DEBUG|-------->LIST POLICIES LAST.ENTRY.DATE GT \7/26/2016\ *OUTPUT* CLIENT.CODE 31 32 33
00:30:43.17 (19180360)|CODE_UNIT_FINISHED|execute_anonymous_apex
00:30:43.17 (20942913)|EXECUTION_FINISHED

Your code should be like below
writer.writeAttribute(null,null,'statement', 'LIST POLICIES LAST.ENTRY.DATE GT \\7/26/2016\\ *OUTPUT* CLIENT.CODE 31 32 33');

Let us know if this will help you