Strings Comparison

5

There are several ways of comparison, but I always see that the most indicated are ( ===, strcmp ou strcasecmp ).

Among these forms considered the safest (according to some websites), the following doubts follow:

  • Which would be the most suitable for use?
  • What's the difference between them?
  • Is there a difference in performance?
  • asked by anonymous 02.04.2015 / 14:50

    2 answers

    7

    You are referring to different approaches. Although both compare two strings which return (or say about strings) is different.

    === returns only a Boolean, true, or false.

    strcmp returns negative ( <0 ) if the first string passed to the function is less than the second; positive otherwise; and zero if they are equal.

    It can be said that === is to know if they are identical, and strcmp is to compare strings returning 3 possibilities. strcasecmp is a variant of strcmp but case-insensitive , ignoring whether it has large or small letters.

    Performance:

    It is only possible to compare performance differences in the case of both in commun, that is, if you want to compare if two strings are identical.

    So, given that strcmp and strcasecmp need another check to know the result it becomes obvious that === is faster. Either strcmp runs first strcmp(strA, strB) and then have to have another equality check == 0 . I have found some numbers here that point to faster 3x performance using === .

    Conclusion:

    If you want to know if two strings are identical, you must use === .

        
    02.04.2015 / 14:56
    -4

    Use === if you want to know if they are identical.

    If you want to know which is the largest of which user strcmp.

    Do not be alone in this post go as far as you can in knowledge.

        
    09.11.2018 / 03:22