Search Unity

problem with strings and paths

Discussion in 'Scripting' started by Gerray2, Jan 15, 2020.

  1. Gerray2

    Gerray2

    Joined:
    Apr 24, 2018
    Posts:
    13
    This code prints true and works just fine:

    Code (CSharp):
    1. if (ImportFrom.Replace("\\", "/").Equals("C:/Users/gfell/Documents/item side projects/3D Viewer/01_ImportFolder"))
    2. {
    3.     Debug.Log("true");
    4. }
    5.  
    6. var info = new DirectoryInfo("C:/Users/gfel/Documents/item side projects/3D Viewer/01_ImportFolder");

    This one does not print true and throws "ArgumentException: Illigal characters in path":

    Code (CSharp):
    1. if (ImportFrom.Replace("\\", "/").Equals("C:/Users/gfell/Documents/item side projects/3D Viewer/01_ImportFolder"))
    2. {
    3.     Debug.Log("true");
    4. }
    5.  
    6. var info = new DirectoryInfo(ImportFrom.Replace("\\", "/"));
    How come?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Did you try printing out what the string is after you do the replace? If it's not printing true, then your ImportFrom string would appear to have an illegal char in it and it doesn't match your path string.
     
  3. Gerray2

    Gerray2

    Joined:
    Apr 24, 2018
    Posts:
    13
    I tested a bit and using a veriable actually does work. so saying this is just fine:

    Code (CSharp):
    1.         ImportFrom = "C:\\Users\\gfell\\Documents\\item side projects\\3D Viewer\\01_ImportFolder";
    2.  
    3.         if (ImportFrom.Replace("\\", "/").Equals("C:/Users/gfell/Documents/item side projects/3D Viewer/01_ImportFolder"))
    4.         {
    5.             Debug.Log("true");
    6.         }
    7.  
    8.         var info = new DirectoryInfo(ImportFrom.Replace("\\", "/"));
    But I want to extract the path out of an txt file. This is what I do for that:

    Code (CSharp):
    1.     public string ImportFrom;
    2.     public string MoveTo;
    3.     public string ExportTo;
    4.  
    5.     public TextAsset txtLinkFile;
    6.  
    7.     void Start()
    8.     {
    9.         setLinks(txtLinkFile.text);
    10.     }
    11.  
    12.     public void setLinks(string text)
    13.     {
    14.         string[] links = text.Split('\n');
    15.         ImportFrom = links[0];
    16.         MoveTo = links[1];
    17.         ExportTo = links[2];
    18.     }
    With this inside my txt file:


    C:\Users\gfell\Documents\item side projects\3D Viewer\01_ImportFolder
    C:\Users\gfell\Documents\item side projects\3D Viewer\00_Archive
    C:\Users\gfell\Documents\item side projects\3D Viewer\02_Exported


    When I print it the console I get the same outputs of ImportFrom.
    Why does the string extracted from an txt file behaves different than the hardcoded?
     
    Last edited: Jan 15, 2020
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    As mentioned, please print out your string for us. Don't use Debug.Log(true), but print out the actual path. Do your replace into a variable, and print it out.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    So in setLinks you should be printing out each value before the replace. After doing the replace you should also be printing out the value.

    Also, just curious, but why do you need to do a replace instead of just including the path with the slashes as you need them?

    Otherwise, if both your printouts seem fine, you may need to loop through the chars in the string and see if you notice anything odd. Depending on how you created the txt file, there are sometimes hidden chars that can be created. You may just consider using notepad and create the txt file again from scratch.
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Make sure you're using the correct separators for the line endings. \n is not necessarily the only character that is used, you also find \r\n or sometimes even \n. This might already solve the problem. If it doesn't, debug the code / log the character values as integers.
     
  7. Gerray2

    Gerray2

    Joined:
    Apr 24, 2018
    Posts:
    13
    This was actually it. :)

    In the console all strings looked as expacted. with \ or / depending on if .Replace was used or not.
    But when I opend the .txt in notepad++ and turned on visualizing all characters, I saw "CR LF" on the line endings. So replacing \r\n with \n in the .txt was the solution.

    Thanks everyone!