-1

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"));

fferri
  • 18,285
  • 5
  • 46
  • 95
Sanjay Verma
  • 1,390
  • 22
  • 40

2 Answers2

3

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!

hagrawal7777
  • 14,103
  • 5
  • 40
  • 70
1

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

chris
  • 1,685
  • 3
  • 18
  • 28