Trying to see if strings have the same char in them?

helo. I’m trying to see if strings have the same char in them. I’ve tried but it didn’t work that well (it said it did have it when it shouldn’t). Here’s the code I tried doing it with, but if someone sees the problem, please comment.

    void Update()
    {

        char[] first = curPressedPattern.ToCharArray();
        char[] second = pattern.ToCharArray();
        if (isSame) {
            for (int i = 0; i < first.Length; i++)
            {
                Debug.Log("Passed first");
                char letter = first[i];
                if (letter == second[i]) {
                    Debug.Log("Passed second");
                    value++;
                }
            }
        if (value >= maxClicks) {
            Debug.Log("Passed third");
            isSame = true;
}

Thanks

Can you explain a bit more what you’re trying to do? Are you trying to find out if the two strings contain ANY shared characters?

Ok, thanks for you quick reply. I’m trying to find out if the two strings contain all the same characters in it. So like an example would be like “abc” and “cba”, both of them have the same characters in them but in different orders. Thanks again for your quick reply! :slight_smile:

Easiest way to do that would be to sort the characters in the strings and then compare the results.

Here’s some example code for sorting the characters in a string: https://www.dotnetperls.com/alphabetize-string
Then you can compare the sorted strings with the == operator.

So for example if you have the strings “cat” and “act”, they would both get sorted as “act” which are the same.

1 Like

Besides just being easier to write, this is a better solution than looping through the contents of the strings, because it will automagically handle multiple instances of the same characters without any issue.

For example, if you compare akasd and kdaas, both should resolve to aadks and use == to compare. If you were to brute force iterate the contents of the strings through, you’d have to worry about comparisons like akasd and kdsa, because if you’re not careful you might get a false positive result (both containing all the same letters, just in different quantity, but you will find an instance of each letter in the other string though).

You could count the number of times each letter of the alphabet appears in each string, the compare the result. This should give you a O(n) solution that scales well with large strings, and shouldn’t need to generate any garbage.

Hey, I tried that. But it didn’t work that well with my code. If you know what I did wrong please tell me.

    void Update()
    {

        char[] first = curPressedPattern.ToCharArray();
        char[] second = pattern.ToCharArray();
        Array.Sort(first);
        Array.Sort(second);

        if (first == second) {
            Debug.Log("It is true");
        } else {
        Debug.Log("It is false");
}

Thanks!

You need to turn them into Strings, as Array == does not do a value comparison it does a reference comparison.

    void Update()
    {
        char[] first = curPressedPattern.ToCharArray();
        char[] second = pattern.ToCharArray();
        Array.Sort(first);
        Array.Sort(second);
        string firstStr = new string(first);
        string secondStr = new string(second);
        if (firstStr == secondStr) {
            Debug.Log("It is true");
        } else {
        Debug.Log("It is false");
    }

Oh, thanks! It works perfectly now. Thanks for your time! :slight_smile: Have a great day or night! :smile: