How to encode the special characters for correct URL String using apex in Salesforce?

How to encode the special characters for correct URL String using apex in Salesforce?

EncodingUtil.urlEncode() encodes the special characters for correct URL String.

Sample Code:

String strURL = 'www.test.com?name=#1solutions';
strURL = EncodingUtil.urlEncode(
    strURL, 'UTF-8'
);
System.debug(
    'Encoded URL is ' +
    strURL
);

Output:

Leave a Reply