Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

'+' sign in a string

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

  1. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    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;
    4.  
    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

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    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 https://docs.unity3d.com/ScriptReference/WWW.UnEscapeURL.html
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    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

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Ok, I understand now.
    Thanks for reply.