devxlogo

Using the URLEncoder/URLDecoder Classes

Using the URLEncoder/URLDecoder Classes

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'
See also  How to Avoid Money Transfer Scams  
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist