Encode String to URL friendly format


I tried to encode a string into URL friendly format but it seems it’s not working correctly.

var username:String = "Test 123"

var encodedUsername:String = WWW.EscapeURL(username);

Debug.Log("Encoded username is: "+encodedUsername);  //The result is "Test+123" instead of supposedly "Test%20123"

while the other set of code I have:

var email:String = "email@email.com"

var encodedEmail:String = WWW.EscapeURL(email);

Debug.Log("Encoded email is: "+encodedEmail);    //The result is "email%40email.com

Is there anything I missed? Thanks in advance.

I thought of manually using Replace(" ","%20") but then this won’t work with other characters and I can’t predict what characters will be used by the user.

Yes, you are missing something.

There are several standards when it comes to URL encoding, each differs from the other by some mostly minor means.

One of the differences is that some standards convert space to a + and some convert space like any other illegal character to %20.

The plus sign is valid in most cases, but I found that it does not work well in some iOS related scenarios, so my workaround is to first do EscapeURL and then replace + with %20

Best answers is here

Have you tried System.Uri.EscapeUriString()?