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

"StringComparison.CurrentCulture" How to tell the outcome?

Discussion in 'Scripting' started by NumLock2020, Jul 19, 2020.

  1. NumLock2020

    NumLock2020

    Joined:
    Feb 6, 2020
    Posts:
    38
    Hello everyone,

    I'm messing around with comparing "strings" from different "grid cells". I have the grind cells marked by numbers and am messing with comparing the cell number "objects" by name and stumbled upon a "string.Compare" method and was wondering where the outcome of the compared method is placed?

    So say it went something like this,
    Code (CSharp):
    1.         public object objects;
    2.  
    3.         public int CompareTo()
    4.         {
    5.             if (objects == null) return 1;
    6.  
    7.             Block checkRightSpace = objects as Block;
    8.             if (checkRightSpace != null)
    9.                 return string.Compare(this.name, checkRightSpace.name, StringComparison.CurrentCulture);
    10.             else
    11.                 throw new ArgumentException("Object is not a MatchPieceType");
    12.         }
    How do I tell if they matched? Something like a debug to see if they did match.
     
  2. bentontr

    bentontr

    Joined:
    Jun 9, 2017
    Posts:
    39
    Hey there,
    string.Compare will return zero when the strings are equal in the sort order. You can check the return value for this case and print out whatever you like. I would caution you, however, that comparing many strings can have a substantial impact on your games performance if you are comparing thousands of them each frame. Stick to integers if you need to make many comparisons.