1

I am sending an Http request who's body contains XML payload. Data sent over can have apostrophes which causes my palyload to error out. XML payload looks something like

<comments>testString</Ccomments>

where testString is a string variable that contains apostrophes.

I tried using

 string.escapeSingleQuotes(testString);

but this replaces ' with \'

I want to replace all apostrophes with &apos;

Any suggestions?

1
  • How about a = a.replace('\'', '&apos;'); Jun 26 '13 at 15:24
3

You can use the escapeHtml4 String method.

String s1 = 
   '"<Black&White>"';
String s2 = 
   s1.escapeHtml4();
System.debug(s2);
// Output:
// &quot;&lt;Black&amp;
// White&gt;&quot;
1

Use EncodingUtil.urlEncode(); it worked for me. http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_classes_restful_encodingUtil.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.