Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

C# Compare Strings?

Discussion in 'Scripting' started by WolfShield, Dec 23, 2011.

  1. WolfShield

    WolfShield

    Joined:
    Feb 7, 2011
    Posts:
    126
    Hello guys,
    I am having an issue with comparing strings in C#. I used the simple:

    Code (csharp):
    1.  
    2. if(str1 == str2){
    3.     doSomething();
    4. }
    5.  
    But it's saying it can't compare types 'string' and 'string[]' with '=='. I looked in the forums and it appears that at one point you were able to, but now I can't? I tried some casts ( 'int' to many periods, 'string[]' couldn't convert ).

    Please let me know what your ideas are.

    Thanks,

    - WolfShield
     
  2. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    Compare them one at a time.

    str1 and str2 are not both strings, one is an array of strings(hence string[]).
    Either compare each element in the string one at a time or figure out why you are mistaking a group of strings with a single string.
     
  3. WolfShield

    WolfShield

    Joined:
    Feb 7, 2011
    Posts:
    126
    So,
    There is no function to just compare strings in Unity's C#?

    Also, when I tried:

    Code (csharp):
    1.  
    2. if(String.Compare(str1, str2) == 0){
    3.     someFunction();
    4. }
    5.  
    It only said that 'String' was not in current context. Does that mean it's an actual function and I just need to include 'String' in it somewhere? Or is that completely wrong?

    Thanks,

    - WolfShield
     
    Last edited: Dec 23, 2011
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    What are you actually trying to do? Do you have an array of strings, and you're trying to see if the string is in the array? If so, use Array.Contains.

    --Eric
     
  5. WolfShield

    WolfShield

    Joined:
    Feb 7, 2011
    Posts:
    126
    It's actually some Networking stuff.
    I'm comparing the comment section of the MasterServer, to the ip address of an element in MasterServer.

    So the comment is of type 'string' and the ip address is of type 'string[]' because of the '.' of course.

    - WolfShield
     
  6. Swearsoft

    Swearsoft

    Joined:
    Mar 19, 2009
    Posts:
    1,632
    They are not of the same type, so I think it is obvious you have to change one or the other before comparing.
     
  7. siegeon

    siegeon

    Joined:
    Nov 23, 2010
    Posts:
    81
    Type string and type string array are two different types, but that is resolved easily enough, something like this should work. keep in mind that you need to include the system namespace for access to String
    Code (csharp):
    1.  
    2.         string[] strings = new string[10];
    3.         string otherString = "whatever";
    4.  
    5.         foreach (string part in strings)
    6.     {
    7.             if (String.Equals(part, otherString))
    8.             {
    9.                 //do something when its true
    10.             }
    11.             else
    12.             {
    13.                 //do something when its not true;
    14.             }
    15.     }
    16.  
     
    Arcarnan likes this.
  8. SevenBits

    SevenBits

    Joined:
    Dec 26, 2011
    Posts:
    1,953
    When you did String.Compare(), it developed an error b/c the class String doesn't exist. It is called string (lowercase 'S').
     
  9. siegeon

    siegeon

    Joined:
    Nov 23, 2010
    Posts:
    81
    No, you need to include system....

    at the top of your file type using System
     
    LagMakesGames likes this.
  10. WolfShield

    WolfShield

    Joined:
    Feb 7, 2011
    Posts:
    126
    Siegion,
    Your solution worked flawlessly. Thank you very much!

    And to everyone who replied, thank you for spending your time sharing your knowledge with me.

    Sincerely,

    - WolfShield
     
    radiantboy likes this.
  11. siegeon

    siegeon

    Joined:
    Nov 23, 2010
    Posts:
    81
    No problem =)
     
  12. firemyst

    firemyst

    Joined:
    Sep 28, 2013
    Posts:
    3
  13. thepenguinmaster

    thepenguinmaster

    Joined:
    May 22, 2013
    Posts:
    29
    You could also use linq. Although many of these responses are great, Linq will do this exact thing for us,. and its a nice clean one liner. All you need to do is include System.Linq.

    In the background, it iwll still do its loop, but is optimised and clean.

    using UnityEngine;
    using System.Collections;
    using System.Linq;

    public class NewBehaviourScript : MonoBehaviour {
    public void someFunc(){
    string[] testArray = {"test1", "test2"};

    string testString = "test1";
    string noMatchString = "test5";

    bool testStringExists = testArray.Contains(testString);
    bool noMatchStringExists = testArray.Contains(noMatchString);
    }
    }
     
    Raptor8 likes this.
  14. jdoonan61

    jdoonan61

    Joined:
    Jun 7, 2015
    Posts:
    13
    what if you were trying to compare a string to an enum return?
     
  15. Chinmay_Gawande

    Chinmay_Gawande

    Joined:
    May 28, 2017
    Posts:
    4
    Cool Go for the first code only..That Worked
     
  16. radiantboy

    radiantboy

    Joined:
    Nov 21, 2012
    Posts:
    1,637
    whats wrong with myString.Equals(myOtherString); is it slow?
     
  17. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,723
    Who said that anything is wrong with String.Equals?
     
  18. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,100
    There is nothing wrong with String.Equals.
    There is a problem with myString.Equals since myString cannot be null.
     
  19. radiantboy

    radiantboy

    Joined:
    Nov 21, 2012
    Posts:
    1,637
    its just no one mentioned it, thats the one that comes to mind instantly maybe because im a java coder by nature.. But my question I guess is what is the fastest way to compare strings? and maye if anyone knows why is compareTag() quicker than comparing manually, it must do something special.
     
  20. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,371
    String equality becomes complicated because there's a bunch of different ways to compare strings. See String.Equals and the StringComparison enum. None of that is slower than what's reasonable for what you're asking for, but if you're doing a culture-sensitive comparison when what you really mean is "do these two strings have the same bytes", you're being slower than you need to.

    String.Equals defaults the culture to CurrentCulture, which means that the behaviour depens on what computer it runs on, and is slower than the Ordinal (byte-by-byte) comparison.

    Note that unlike Java, C# can override ==, so in C# == for strings checks content (byte) equality, not reference equality like in Java. So in C#, str1 == str2 is the same as str1.Equals(str2, StringComparison.Ordinal).

    So in the vast, vast, vast majority of cases, str1 == str2 is what you're looking for. In Java, str1 == str2 is almost always a bug.

    CompareTag knows that there's a limited number of tags, and that there's no reason to care about any of that stuff, so it just compares the tag's hash codes.