+ Start a Discussion
Jerun JoseJerun Jose 

Display pipe symbol in Apex string

Hi,


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
USER_DEBUG|[1]|DEBUG||
 
Any help is appreciated.

Raj.ax1558Raj.ax1558

try this -

string str = '\|';

 

 

Thankyou.

 

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?

amitashtekaramitashtekar

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.

 

Thanks!

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';
system.debug('@@@'+str);
system.debug('@@@'+str.contains('|'));
system.debug('@@@'+str.contains(',|'));
system.debug('@@@'+str.replaceAll(',|','|'));
system.debug('@@@'+str.replaceAll('|',':'));

 This gives the output as

 

Can someone help me understand why it works this way?

amitashtekaramitashtekar

Hi Jerun,

 

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

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_string.htm

 

Please let me know if you have any query.

 

Thanks!

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,|';

system.debug('@@@'+str.replaceAll(',\\|','|'))

 

Will produce 

 

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

 

Hope that helps

dannapareddy1.3909373019751619E12dannapareddy1.3909373019751619E12
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');
        if(!string.isblank(s)){
            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.

Abhijeet
Alex KolyadichAlex Kolyadich
dataString.split('\\|');