Search Unity

Unity is ignoring 0's in string

Discussion in 'Scripting' started by OmarDajani, Apr 19, 2019.

  1. OmarDajani

    OmarDajani

    Joined:
    Aug 17, 2016
    Posts:
    49
    Does anyone know how I can avoid Unity removing 0's before a string?
    For example: I have an integer 0000012, when Unity processes this number it becomes 12 whether it is an integer or string. How can I adjust this so that I can keep the zeros?
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Code (CSharp):
    1. string yourString = String.Format("00000000", yourInt);
     
  3. OmarDajani

    OmarDajani

    Joined:
    Aug 17, 2016
    Posts:
    49
    This is not working. This would work for a while until profiles become a large number. For example:
    The first player to register would have the ID 0000001
    The second player to register would have the ID 0000002

    The problem arises when I have lets say 200 players...
    The ID would be 0000200

    This could would not work. The ID numbers that are being sent to Unity are from a PHP script that increments the ID automatically. So I need the ID number to be sent to Unity without the zeros being tampered with.

    Thanks for your reply!
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Don't convert the ID to an int. You'll need it to be a string. Unity doesn't ignore 0s, but to an int, 000012 and 12 are the same thing so it has no reason to keep the starting 0s.

    It might also help to understand better what you are doing. Do you just not know how many zeros the ID starts with?
     
  5. OmarDajani

    OmarDajani

    Joined:
    Aug 17, 2016
    Posts:
    49
    Sorry, I was a bit unclear in my original post. I am reading it as a STRING, let's say the string is 0000012, however, when I come to print that string in the console it prints as 12 and not as 0000012.

    To answer your second question, the number of zeros varies on the ID number. If the ID number is 200 then I'd need four zeros before 200, if the ID is 1200 I'd need 3 zeros before 1200.
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    Can you show us this code?

    Because Unity shouldn't be ignoring leading zero's in a string. Strings are strings... they're not #'s. They make no distinction between what the characters are in the string. If they're there, they're there.
     
  7. OmarDajani

    OmarDajani

    Joined:
    Aug 17, 2016
    Posts:
    49
    Sure. Here is my code:

    Code (CSharp):
    1. Debug.Log("TEST ID: " + Tools.GetDataValue(www, "ID:"));
    2. public static string GetDataValue(string data, string index) {
    3.         string value = data.Substring(data.IndexOf(index) + index.Length);
    4.         if (value.Contains("|")) {
    5.             value = value.Remove(value.IndexOf("|"));
    6.         }
    7.         if (value.Contains(";")) {
    8.             value = value.Remove(value.IndexOf(";"));
    9.         }
    10.         return value;
    11.     }
    Credit to Creagines for the GetDataValue function that I am using for now.

    When I am printing the ID from the database it is in the format of "|ID: 0000012" which is why I parse the string, after/before parsing the string is 12.
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    So if you Debug.Log(www) is the value 12 or 00000012?

    I assume www is the response string you're getting back before you do any formatting to it.
     
  9. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    So your saying that 'www' is "12" before you even call 'GetDataValue'?

    Can I see how you retrieve 'www' then?
     
  10. OmarDajani

    OmarDajani

    Joined:
    Aug 17, 2016
    Posts:
    49
    The www was the problem! PHP removed the padding zeros to begin with which is why Unity wasn't displaying them. Thank you for your help! :)