String comparison using '==' vs. 'strcmp()'
The reason to use it is because strcmp
returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.
===
only returns true
or false
, it doesn't tell you which is the "greater" string.
strcmp or string::compare?
For C++, use std::string
and compare using string::compare
.
For C use strcmp
. If your (i meant your programs) strings (for some weird reason) aren't nul
terminated, use strncmp
instead.
But why would someone not use something as simple as ==
for std::string
?
Why use strcmp instead of == in C++?
strcmp
compares the actual C-string
content, while using ==
between two C-string
is asking if these two char
pointers point to the same position.
If we have some C-string
defined as following:
char string_a[] = "foo";
char string_b[] = "foo";
char * string_c = string_a;
strcmp(string_a, string_b) == 0
would return true
, while string_a == string_b
would return false
. Only when "comparing" string_a
and string_c
using ==
would return true
.
If you want to compare the actual contents of two C-string
but not whether they are just alias of each other, use strcmp
.
For a side note: if you are using C++
instead of C
as your question tag shows, then you should use std::string
. For example,
std::string string_d = "bar";
std::string string_e = "bar";
then string_d == string_e
would return true
. string_d.compare(string_e)
would return 0
, which is the C++
version of strcmp
.
Storing/Comparing u_char passed to function
snprintf()
will use the same format string and parameters as the printf()
call you already have working.
char NewString[] = "00:00:00:00:00:00"; // initializing with this string is just a lazy
// way to get the right amount of storage
snprintf( NewString, sizeof( NewString), "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x", var[0], var [1], var[2], var[3], var[4], var[5]);
Using strcmp to compare strings vs. comparing characters directly
Comparing characters directly is pretty vile and fragile code. Depending on the compiler and architecture, it might also be harder to optimize.
On the other hand, your copy is a waste - it does nothing useful.
Just check the string is at least long enough (or exactly the right length, but any way not too short) and strncmp
(or memcmp
) it in place.
#define COMPARE(IN, OFF, SUB) memcmp(IN+OFF, SUB, sizeof(SUB)-1)
input = "BANANASingorethispartAPPLESignorethisalsoORANGES";
if (COMPARE(input, 0, "BANANAS") == 0 &&
COMPARE(input, 21, "APPLES" ) == 0 &&
COMPARE(input, 40, "ORANGES") == 0) )
{
String.equals versus ==
Use the string.equals(Object other)
function to compare strings, not the ==
operator.
The function checks the actual contents of the string, the ==
operator checks whether the references to the objects are equal. Note that string constants are usually "interned" such that two constants with the same value can actually be compared with ==
, but it's better not to rely on that.
if (usuario.equals(datos[0])) {
...
}
NB: the compare is done on 'usuario' because that's guaranteed non-null in your code, although you should still check that you've actually got some tokens in the datos
array otherwise you'll get an array-out-of-bounds exception.
Comparing two strings, problems with strcmp
strcmp
returns 0 if the two strings are exactly the same to accomplish what you want to do
Use :
strstr(s2 , "login:")
(It return NULL
if the string doesn't exist in s2
)
or
strncmp(s2 , "login:" , 6)
This will compare the first 6 characters (if s2
begins with "login:" , it will return 0)
Related Topics
Login to Remote Site With PHP Curl
How to Remove "Public/Index.PHP" in the Url Generated Laravel
PHP: Is MySQL_Real_Escape_String Sufficient For Cleaning User Input
How to Get the Base Url With PHP
How to Remove File Extension from a Website Address
Tracking the Script Execution Time in PHP
Importing CSV Data Using PHP/MySQL
Fastest Way to Check If a String Is Json in PHP
How to Enable Curl in PHP/Xampp
How to Measure the Speed of Code Written in PHP
Search With Comma-Separated Value MySQL
How to Refresh Token With Google API Client
How to Add Http:// If It Doesn't Exist in the Url
How to Remove Line Breaks (No Characters!) from the String