user3573403
user3573403

Reputation: 1802

Encode URL with US-ASCII character set

I refer to the following web site:

http://coderstoolbox.net/string/#!encoding=xml&action=encode&charset=us_ascii

Choosing "URL", "Encode", and "US-ASCII", the input is converted to the desired output.

How do I produce the same output with Java codes?

Thanks in advance.

Upvotes: 1

Views: 2442

Answers (2)

anacron
anacron

Reputation: 6711

I used this and it seems to work fine.

public static String encode(String input) {
    Pattern doNotReplace = Pattern.compile("[a-zA-Z0-9]");
    
    return input.chars().mapToObj(c->{
        if(!doNotReplace.matcher(String.valueOf((char)c)).matches()){
            return "%" + (c<256?Integer.toHexString(c):"u"+Integer.toHexString(c));
        }
        return String.valueOf((char)c);
    }).collect(Collectors.joining("")).toUpperCase();
}

PS: I'm using 256 to limit the placement of the prefix U to non-ASCII characters. No need of prefix U for standard ASCII characters which are within 256.


Alternate option:

There is a built-in Java class (java.net.URLEncoder) that does URL Encoding. But it works a little differently (For example, it does not replace the Space character with %20, but replaces with a + instead. Something similar happens with other characters too). See if it helps:

String encoded = URLEncoder.encode(input, "US-ASCII");

Hope this helps!

Upvotes: 1

Rishav Mishra
Rishav Mishra

Reputation: 116

You can use ESAPi.encoder().encodeForUrl(linkString)

Check more details on encodeForUrl https://en.wikipedia.org/wiki/Percent-encoding

please comment if that does not satisfy your requirement or face any other issue.

Thanks

Upvotes: 0

Related Questions