Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

'+' sign in a string

Discussion in 'Scripting' started by herbie, Feb 22, 2017.

  1. herbie


    Feb 11, 2012
    Hi everybody,

    I am busy with the Highscores tutorial with dreamlo on youtube:
    I have a little problem with displaying the username in the highscorelist.
    The usename is a string but when my username consist of two words separated by a space (first name and last name), a '+' sign is placed beween the two words when it is displayed in the scoreboard.

    I think it's in this code:
    Code (CSharp):
    1. IEnumerator UploadNewHighscore(string username, int score) {
    2.         WWW www = new WWW(webURL + privateCode + "/add/" + WWW.EscapeURL(username) + "/" + score);
    3.         yield return www;
    5.         if (string.IsNullOrEmpty(www.error)) {
    6.             print ("Upload Successful");
    7.             DownloadHighscores();
    8.         }
    9.         else {
    10.             print ("Error uploading: " + www.error);
    11.         }
    12. }
    Does anybody knows where this '+' sign is coming from and how I can replace it by a space?
  2. Brathnann


    Aug 12, 2014
    I think it's because you're using it with escapeURL. Certain chars get changed when you use that to make them URL compatible. I bet if you debugged it it might show a + (maybe, this is a guess)

    If you are displaying it local, just use the string.replace call to replace + with a space.

    Code (CSharp):
    1. String userName = "My+Name";
    2. userName= userName.Replace('+', ' ');
    Code should do it, but didn't test it, note it's a reference point since you'll have to replace the value depending on where your username comes from.

    Unity also provides an unEscapeUrl that may just reverse the name
  3. StarManta


    Oct 23, 2006
    It's from escaping the URL. Since spaces aren't used in URL's, + is one of two standards for escaping it (the other being %20). And you do, absolutely, need to escape the url before you submit it - otherwise you'll be leaving your game open to both bugs and security flaws if players start using weird characters in their names.

    What you need to do is to un-escape the returned text before you display it on the scoreboard.
  4. herbie


    Feb 11, 2012
    Ok, I understand now.
    Thanks for reply.