Urlencoding and toLowerCase - effect on output

507 views Asked by At

I came through this code (java), and was wondering if below two will have any actual difference in output:

  1. String output1 = (URLEncoder.encode(plainString, "UTF-8")).toLowerCase();

  2. String output2 = URLEncoder.encode(plainString.toLowerCase(), "UTF-8"));

2

There are 2 answers

0
hagrawal7777 On BEST ANSWER

First question why do you want to do lower case? URLs are case sensitive.

To answer your question - yes, there will be difference.

Using UTF-8 as the encoding scheme the string "The string ü@foo-bar" would get converted to "The+string+%C3%BC%40foo-bar" because in UTF-8 the character ü is encoded as two bytes C3 (hex) and BC (hex), and the character @ is encoded as one byte 40 (hex).

Now, in your this case - (URLEncoder.encode(plainString, "UTF-8")).toLowerCase() Hexa-decimal values will be converted to lower case.

Consider below example:

        String output1 = (URLEncoder.encode("ü@foo-Bar", "UTF-8")).toLowerCase();
        String output2 = URLEncoder.encode("ü@foo-Bar".toLowerCase(), "UTF-8");
        System.out.println(output1);
        System.out.println(output2);

Output:

%c3%bc%40foo-bar
%C3%BC%40foo-bar

Hope this helps!

0
chris On

The results could differ: i tried it with "Ü" (german Umlaut) and output1 is %c3%9c whereas output2 is %C3%BC.