C++ Compare char array with string

Use strcmp() to compare the contents of strings: if (strcmp(var1, “dev”) == 0) { } Explanation: in C, a string is a pointer to a memory location which contains bytes. Comparing a char* to a char* using the equality operator won’t work as expected, because you are comparing the memory locations of the strings rather … Read more

Comparing strings with tolerance

You could use the Levenshtein Distance algorithm. “The Levenshtein distance between two strings is defined as the minimum number of edits needed to transform one string into the other, with the allowable edit operations being insertion, deletion, or substitution of a single character.” – Wikipedia.com This one is from dotnetperls.com: using System; /// <summary> /// … Read more

String Comparison in Java

Leading from answers from @Bozho and @aioobe, lexicographic comparisons are similar to the ordering that one might find in a dictionary. The Java String class provides the .compareTo () method in order to lexicographically compare Strings. It is used like this “apple”.compareTo (“banana”). The return of this method is an int which can be interpreted … Read more

String comparison and String interning in Java

You should almost always use equals. You can be certain that string1 == string2 will work if: You’ve already made sure you’ve got distinct values in some other way (e.g. you’re using string values fetched from a set, but comparing them for some other reason) You know you’re dealing with compile-time string constants You’ve manually … Read more