How to decode the URL

How to decode the URL

Example:

Input:  https://beingcodeexpert.blogspot.com/?q=Java%20Is%20programming%20language
Output: https://beingcodeexpert.blogspot.com/?q=Java Is programming language

Approach

Java


import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URLDecoder;

public class EncodeURL {
    public static void main(String[] argsthrows URISyntaxExceptionMalformedURLException {
        String url = "https://beingcodeexpert.blogspot.com/?q=Java%20Is%20programming%20language";
        String decodeURL = decode(url);
        System.out.println("Decoded URL: " + decodeURL);

    }

    public static String decode(String url) {
        try {
            String prevURL = "";
            String decodeURL = url;
            while (!prevURL.equals(decodeURL)) {
                prevURL = decodeURL;
                decodeURL = URLDecoder.decode(decodeURL, "UTF-8");
            }
            return decodeURL;
        } catch (UnsupportedEncodingException e) {
            return "Issue while decoding" + e.getMessage();
        }
    }
}



No comments:

Post a Comment