You need to sign in to do that
Don't have an account?
Jerun 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.
try this -
string str = '\|';
Thankyou.
Please mark this as solution for others to get it easily
I am getting the error Invalid string literal.
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.
Thanks!
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
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.
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_string.htm
Please let me know if you have any query.
Thanks!
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
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];
}
}
Your solution worked for me like a charm.
Thanks a lot.
Abhijeet
https://salesforced.wordpress.com/2012/05/16/using-the-string-split-method-with-a-pipe-delimiter/