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

System.IO -> file.name ... UTF8-problem?

Discussion in 'Scripting' started by JoergLange, Mar 3, 2016.

  1. JoergLange

    JoergLange

    Joined:
    Oct 22, 2015
    Posts:
    21
    Hi folks,

    maybe a silly question...

    For example I'm using

    foreach(FileInfo file in fi.GetFiles()){
    ...some code
    }


    Then I get the name of the file in file.name .

    Lets asume the filename is something with ä,ö,ü, for example file.name is zündhölzer.jpg

    When I now search in this filename like this:

    string sourceString = file.name; (file.name is zündhölzer.jpg)
    string searchString = "zündhölzer.jpg"

    if (sourceString.Contains(searchString)) {

    ....
    }


    ...will not work. It doesnt find the searchString, even if sourceString and searchString are equal. This is only the case, when the searchString comes from a file.info, so I asume the probleme has something to do with encoding in the System.IO ??

    Best regards
    Jörg
     
  2. SneeKeeFahk

    SneeKeeFahk

    Joined:
    Jan 25, 2015
    Posts:
    26
    https://msdn.microsoft.com/en-us/library/cc190529(v=vs.110).aspx

    Code (CSharp):
    1.         string fileName = System.IO.Path.GetFileName(file.FullName);
    2.         int test = String.Compare(fileName, searchString, StringComparison.OrdinalIgnoreCase);
    3.         if(test == 0){
    4.             // your code
    5.         }
     
    JoergLange and JohnnyA like this.
  3. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,039
    What they said ^

    You wont be able to use StringComparison with Contains but you can use IndexOf for the same effect.
     
    JoergLange likes this.
  4. JoergLange

    JoergLange

    Joined:
    Oct 22, 2015
    Posts:
    21
    Wow... that was fast. Thank you both.

    Finally you pushed me into the right direction. I now use

    int found = sourceString.IndexOf(searchString, System.StringComparison.InvariantCulture);
     
  5. SneeKeeFahk

    SneeKeeFahk

    Joined:
    Jan 25, 2015
    Posts:
    26

    You will bump into problems using IndexOf like that with the InvariantCulture. Here is an extension method that will return a bool for you and will properly us String.Compare

    Code (CSharp):
    1.  
    2. public static class Extensions{
    3.  
    4.     public static bool IsCultureInvariantMatch(this string val, string check){
    5.         int test = String.Compare(val, check, StringComparison.OrdinalIgnoreCase);
    6.         return test == 0;
    7.     }
    8.  
    9.     public static bool IsFileNameMatch(this System.IO.FileInfo info, string check){
    10.         return info.FullName.IsCultureInvariantMatch(check);
    11.     }
    12.  
    13. }
    14.  
    here is a usage example, obviously you would use your variables and not hard coded strings;

    Code (CSharp):
    1.  
    2. // file is supposed to be your FileInfo object in your loop
    3. Console.WriteLine(file.IsFileNameMatch("zündhölzer.jpg"));
    4. Console.WriteLine("zündhölzer.jpg".IsCultureInvariantMatch("zündhölzer.jpg"));
    5.  
    and here is a DotNetFiddle for you demonstrating how it works:
    https://dotnetfiddle.net/XIXT7C


    Sidenote: It is very important in your case to use OrdinalIgnoreCase, ordinal aspect of that is what will match your file names consistently. read more about that here: https://msdn.microsoft.com/en-us/library/system.stringcomparer.ordinalignorecase(v=vs.110).aspx
     
    JoergLange likes this.
  6. JoergLange

    JoergLange

    Joined:
    Oct 22, 2015
    Posts:
    21
    Thank you very much.

    My problem is, I dont need no check if the strings match exactly (like in the string.compare-example) but some kind of search inside the string, for example: is "ündhö" part of the string "zündhölzer", one could call it a "full text search". So for this purpose it seemed to me that the indexOf ist the right solution (and at the moment it works perfectly fine), whenever it gives back a value bigger than -1, the search-string is somewhere part of the source-string, and thats all I wanna know. However I will study your links, to dive deeper into this stuff. It's not my domain at the moment, all the file/search stuff is part of the not-so-fancy-stuff in my software... ;-)
     
  7. SneeKeeFahk

    SneeKeeFahk

    Joined:
    Jan 25, 2015
    Posts:
    26
    OK, that makes more sense. you should still use OrdinalIgnoreCase instead of just CultureInvariant
     
  8. JoergLange

    JoergLange

    Joined:
    Oct 22, 2015
    Posts:
    21
    Ok, I'll do that. Many thanks for your fast, friendly and competent responses!