To include special characters such as spaces and quotes in a URL string, you need to encode them. Similarly, you need to decode encoded characters in a URL to recover the original URL. The following code demonstrates how to code/decode a string using the URLEncoder/URLDecoder classes.
import java.net.*;public class URL_Encoder_Decoder{public static void main(String[] args){ String sir="@sir de caractere@,nr. 1290 'paragraf 3'"; try{ String sir_codat=URLEncoder.encode(sir,"UTF-8"); String sir_decodat=URLDecoder.decode(sir_codat,"UTF-8"); System.out.println(sir); System.out.println(sir_codat); System.out.println(sir_decodat); }catch(java.io.UnsupportedEncodingException e) {System.out.println("Eroare:"+e.getMessage());} }}
Here’s the result:
@sir de caractere@,nr. 1290 'paragraf 3'%40sir+de+caractere%40%2Cnr.+1290+%27paragraf+3%27@sir de caractere@,nr. 1290 'paragraf 3'