+ Start a Discussion
JamesSSJamesSS 

How to find and replace single quote in string?

String s = 'This's is correct and it's ready to replace';

How to replace the single quote ?

 

replace character : '

Replacing with : '\

My expected output:

 

This\'s is correct and it\'s ready to replace

 

Is it possible?

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

How about using JSENCODE ?

 

$(document).ready(function() {

alert('{!JSENCODE(result)}');
});

All Answers

songlansonglan

Try the String method escapeSingleQuotes 

JamesSSJamesSS

I have used escapeSingleQuotes. I am using json list to Page.That's why i got the error.Is any possible escapeSingleQuotes in Json List?

Avidev9Avidev9
Post the code I guess. That will be really helpful.
You were looking for a string method ? I guess escapeSingleQuotes does what u need
JamesSSJamesSS

My output Json List:

 

[{"Name":"Test'","Phone":"9000000010"},{"Name":"Test'","Phone":"9000000010"}]

 

In above output, Name Contains Name+'(Test')

My code:

public String result{get;set;}   

 

public Test() { 

     List<Account> AccountList = [SELECT Id,Name FROM Account];

     result= Json.serialize(TopAccountsInnerList); 

}

 

<apex:page controller="Test">

<apex:includescript value="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"/>

<script type="text/javascript" charset="UTF-8">

$(document).ready(function() {

alert('{!result}');
});
</script>

</apex:page>

 

Avi How to escape characters in string for passing Json list in page?

Avidev9Avidev9

How about using JSENCODE ?

 

$(document).ready(function() {

alert('{!JSENCODE(result)}');
});

This was selected as the best answer
JamesSSJamesSS

Avi Its working.But it passing into parseJSON, its thorws unexpected string error.

 

My code:

 

var result = $.parseJSON('{!JSENCODE(result)}');

Avidev9Avidev9
James! I will suggest you to use JS remoting instead of merge fields. All of this encoding/decoding into js object and apex object are handeled automatically. You just have to return values like any other method.

http://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting.htm
JamesSSJamesSS

Thanks Avi