Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Splitting a string into an array - help

Discussion in 'Scripting' started by Shadowing, Jan 18, 2019.

  1. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Ran into something wierd today where Debug.Log() seems to be failing where its not logging anything. As no log added at all. not even a empty one.

    It seems Split is giving a error making Debug.Log Fail but yet
    I'm able to read the first key in the array just fine.
    Getting length on the array doesn't work as well.

    Code (csharp):
    1.  
    2.  
    3. string log = "56/192 bleh bleh bleh bleh bleh";
    4.  
    5.  
    6.                         string logString = log.Split(' ').First().Trim();
    7.  
    8.                         Debug.Log(logString); // This 56/192 returns correctly
    9.  
    10.                         string[] strArray = logString.Split('/');
    11.  
    12.                         Debug.Log(strArray); // does not log
    13.  
    14.                         float OverallPercentage = int.Parse(strArray[0].Trim()) / int.Parse(strArray[1].Trim());
    15.  
    16.                         Debug.Log(strArray.Length+" is the length"); // does not log
    17.  
    18.                         Debug.Log(strArray[0].Trim()); // This 56 returns correctly
    19.  
    20.                         Debug.Log(strArray[1].Trim()); // does not log
    21.  
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Just a guess, but it might be seeing the single / character as an escape character. Try putting two //, or use @('/') Also, I generally add some text in each Debug.Log statement in addition to the variables, in case the variables are null or empty.
     
  3. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Thanks for the reply man.

    Turns out Trim is causing the issue some how.

    Code (csharp):
    1.  
    2. Debug.Log(strArray[1]); // This logs 195
    3. Debug.Log(strArray[1].Trim()); // This doesn't log
    4.  
     
    Last edited: Jan 18, 2019
  4. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    This won't log either @JeffDUnity3D


    Code (csharp):
    1.  
    2. Debug.Log("Some text"+strArray[1].Trim()+" Some text"); // does not log
    3.  
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    "\" is the escape character, not "/". Anyway it works fine; try putting the script into a new empty project.

    --Eric
     
  6. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    one thing i just noticed is i forgot to cast the int math to a float.
    Decided to just remove the trim. Really don't need it if im parsing to int.
    Code (csharp):
    1.  
    2. float OverallPercentage = (float)int.Parse(strArray[0]) / int.Parse(strArray[1]);
    3.