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
Jerun JoseJerun Jose 

Display pipe symbol in Apex string


I am trying to insert a pipe symbol in a string for using the google chart apis. Unfortunately, SFDC is replacing the | symbol with |
Is there a way that we can have a string which includes the | symbol?
Doing system.debug('|'); in the developer console displays the output as
Any help is appreciated.


try this -

string str = '\|';





Please mark this as solution for others to get it easily



Jerun JoseJerun Jose
The above line does not compile.
I am getting the error Invalid string literal.
Thomas DvornikThomas Dvornik

We purposly escape '|' in apex debug logs because we use it as a token. i.e. it seperates the timestamp, from the event, from the details, etc. It shouldn't escape it when evaluating apex. Can you include the code that is failing for google api?


Hi Jerun, I have executed line posted by you within execute anonymous section of developer console within chrome browser. It is not displaying any such character mentioned by you.


if require I can share the screenshot as well.


Can you provide more information about it.



Jerun JoseJerun Jose

Thanks for the responses. After some further analysis, I realized that whenever the | symbol is to be shown in the debug log format, it is displayed as |
However, if you use the developer console and view it, it will show the proper pipe symbol.

My actual aim was to do a string replace for ',|' with '|'

That is when I ran into these issues. I feel that the | symbol has a special behaviour in the replaceAll function, maybe because it falls as part of some regex. Consider the following code


String str = ',|test';

 This gives the output as


Can someone help me understand why it works this way?


Hi Jerun,


For replaceAll method of string first parameter is regular expression but not the normal string.


Please let me know if you have any query.



Thomas DvornikThomas Dvornik

amitashtekar is right. The importnat thing to note is that '|' is a regex operator for OR. Your regular expession is saying ",' OR ''. That is why you are getting '|' in between your letters. You will need to escape '|'.


For example,


String str = ',|test|test,|test,|';



Will produce 


09:59:15:153 USER_DEBUG [8]|DEBUG|@@@|test|test|test|


Hope that helps

Is this solved i have s,ilar issue with pipe . If solved can you post the solution
Rajesh Patel 15Rajesh Patel 15
try this solution.
        string s =  string.valueof('1.2.840.114398.1.4408.1|191870');
            string[] ss = EncodingUtil.urlEncode(s,'UTF-8').replace('%7C',' ').split(' ');
            if(ss.size()> 1){
                strsystem = ss[0];
                strvalue = ss[1];        
Abhijeet Anand 23Abhijeet Anand 23
@ Rajesh Patel
Your solution worked for me like a charm.
Thanks a lot.

Alex KolyadichAlex Kolyadich
Here is the solution to this problem: