[SalesForce] Any apex method equivalent to encodeURIComponent as that of JavaScript

We are replacing all the javascript buttons to custom VF buttons.
Have used encodeURIComponent methods in Javascript, unable to find the equivalent salesforce method

Have checked EncodingUtil Class in salesforce but that doesn't seem to solve the purpose ?

Input :

 var uri = "http://w3schools.com/my test.asp?name=ståle&car=saab";
    var res = encodeURIComponent(uri);

Output :

http%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab

Thanks for the help !

Best Answer

Did you check:

String encoded = EncodingUtil.urlEncode(url, 'UTF-8');

Reference: EncodingUtil

Example:

String url = 'http://w3schools.com/my test.asp?name=ståle&car=saab';
String encoded = EncodingUtil.urlEncode(url, 'UTF-8');
System.debug(' Encoded URL: '+encoded);

Result:

Encoded URL: http%3A%2F%2Fw3schools.com%2Fmy+test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab

Related Topic