Questions tagged [string-comparison]

string-comparison is the action of comparing strings, resulting in a boolean or an integer indicating the "distance" between the strings.

String comparison is the action of comparing strings.

The result of a string comparison may be a boolean or an integer; if it is an integer, then this measures the distance between the two strings, usually lexicographic distance. Note that this means that the strings are equal when the comparison yields 0.

There are some issues to be considered when comparing strings. First, there is the underlying type and encoding of the strings (Unicode? 8-byte chars? 16-byte chars?). Second, there is the question if case is relevant.

A classical mistake for beginning programmers is to compare strings using ==, or whatever other operator their programming language uses for comparing primitive types. In some languages, like Java and C, this will compare the instance of the strings (their reference or memory address, respectively), instead of the string itself.

Most programming languages provide built-in functions for comparing strings.

2237 questions
111
votes
0 answers

How to compare 'μ' and 'µ' in C#

I fall into a surprising issue. I loaded a text file in my application and I have some logic which compares the value having µ. And I realized that even if the texts are same the compare value is false. Console.WriteLine("μ".Equals("µ")); //…
D J
  • 6,908
  • 13
  • 43
  • 75
110
votes
7 answers

Why does PHP convert a string with the letter E into a number?

Why does the following statement return true? "608E-4234" == "272E-3063" I have also tried this with single quotes around the strings. The only way I can get it to evaulate to false is by using the === operator instead of == My guess is PHP is…
Andy
  • 4,901
  • 5
  • 35
  • 57
109
votes
16 answers

How can I compare strings in C using a `switch` statement?

In C there is a switch construct which enables one to execute different conditional branches of code based on an test integer value, e.g., int a; /* Read the value of "a" from some source, e.g. user input */ switch (a) { case 100: // Code …
Niklas
  • 1,177
  • 2
  • 8
  • 6
102
votes
10 answers

How to compare Unicode characters that "look alike"?

I fall into a surprising issue. I loaded a text file in my application and I have some logic which compares the value having µ. And I realized that even if the texts are same the compare value is false. Console.WriteLine("μ".Equals("µ")); //…
D J
  • 6,908
  • 13
  • 43
  • 75
100
votes
17 answers

Version number comparison in Python

I want to write a cmp-like function that compares two version numbers and returns -1, 0, or 1 based on their compared values. Return -1 if version A is older than version B Return 0 if versions A and B are equivalent Return 1 if version A is newer…
Johannes Charra
  • 29,455
  • 6
  • 42
  • 51
99
votes
14 answers

How do I compare two strings in python if order does not matter?

I have two strings like string1="abc def ghi" and string2="def ghi abc" How to get that this two string are same without breaking the words?
user3064366
  • 1,509
  • 2
  • 11
  • 15
96
votes
12 answers

Case Insensitive String Comparison in C

I have two postcodes char* that I want to compare, ignoring case. Is there a function to do this? Or do I have to loop through each use the tolower function and then do the comparison? Any idea how this function will react with numbers in the…
bond425
  • 1,077
  • 1
  • 7
  • 12
93
votes
7 answers

Is == in PHP a case-sensitive string comparison?

I was unable to find this on php.net. Is the double equal sign (==) case sensitive when used to compare strings in PHP?
user374343
91
votes
14 answers

Compare version numbers in Objective-C

I am writing an application that receives data with items and version numbers. The numbers are formatted like "1.0.1" or "1.2.5". How can I compare these version numbers? I think they have to be formatted as a string first, no? What options do I…
mlecho
  • 1,149
  • 2
  • 10
  • 14
85
votes
8 answers

String Comparison in Java

What does "compare two strings lexicographically" mean?
Harshana
  • 7,297
  • 25
  • 99
  • 173
84
votes
7 answers

Understanding NSString comparison

Both the following comparisons evaluate to true: 1) @"foo" == @"foo"; 2) NSString *myString1 = @"foo"; NSString *myString2 = @"foo"; myString1 == myString2; However, there are definitely times where two NSStrings cannot be compared using the…
Yarin
  • 173,523
  • 149
  • 402
  • 512
81
votes
6 answers

PostgreSQL: Case insensitive string comparison

Is there a simple ignore-case-comparison for PostgreSQL? I want to replace: SELECT id, user_name FROM users WHERE lower(email) IN (lower('adamB@a.com'), lower('eveA@b.com')); With something like: SELECT id, user_name FROM users …
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
81
votes
2 answers

When to use which fuzz function to compare 2 strings

I am learning fuzzywuzzy in Python. I understand the concept of fuzz.ratio, fuzz.partial_ratio, fuzz.token_sort_ratio and fuzz.token_set_ratio. My question is when to use which function? Do I check the 2 strings' length first, say if not similar,…
Pot
  • 823
  • 1
  • 8
  • 8
81
votes
6 answers

What are some algorithms for comparing how similar two strings are?

I need to compare strings to decide whether they represent the same thing. This relates to case titles entered by humans where abbreviations and other small details may differ. For example, consider the following two titles: std::string first =…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
71
votes
6 answers

Comparing strings with tolerance

I'm looking for a way to compare a string with an array of strings. Doing an exact search is quite easy of course, but I want my program to tolerate spelling mistakes, missing parts of the string and so on. Is there some kind of framework which can…
Oliver Hanappi
  • 12,046
  • 7
  • 51
  • 68