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.

1968 questions
947
votes
9 answers

What is the correct way to check for string equality in JavaScript?

What is the correct way to check for equality between Strings in JavaScript?
JSS
  • 9,479
  • 3
  • 13
  • 3
601
votes
9 answers

Difference between InvariantCulture and Ordinal string comparison

When comparing two strings in c# for equality, what is the difference between InvariantCulture and Ordinal comparison?
Kapil
  • 8,423
  • 10
  • 35
  • 48
511
votes
32 answers

Check whether a string is not null and not empty

How can I check whether a string is not null and not empty? public void doStuff(String str) { if (str != null && str != "**here I want to check the 'str' is empty or not**") { /* handle empty string */ } /* ... */ }
user405398
470
votes
5 answers

Checking whether a string starts with XXXX

I would like to know how to check whether a string starts with "hello" in Python. In Bash I usually do: if [[ "$string" =~ ^hello ]]; then do something here fi How do I achieve the same in Python?
John Marston
  • 9,869
  • 6
  • 20
  • 17
419
votes
13 answers

Getting the closest string match

I need a way to compare multiple strings to a test string and return the string that closely resembles it: TEST STRING: THE BROWN FOX JUMPED OVER THE RED COW CHOICE A : THE RED COW JUMPED OVER THE GREEN CHICKEN CHOICE B : THE RED COW JUMPED…
330
votes
7 answers

MySQL query String contains

I've been trying to figure out how I can make a query with MySQL that checks if the value (string $haystack ) in a certain column contains certain data (string $needle), like this: mysql_query(" SELECT * FROM `table` WHERE…
arik
  • 23,480
  • 35
  • 91
  • 147
311
votes
11 answers

How can I make SQL case sensitive string comparison on MySQL?

I have a function that returns five characters with mixed case. If I do a query on this string it will return the value regardless of case. How can I make MySQL string queries case sensitive?
StevenB
  • 3,145
  • 2
  • 13
  • 4
292
votes
10 answers

How do I compare version numbers in Python?

I am walking a directory that contains eggs to add those eggs to the sys.path. If there are two versions of the same .egg in the directory, I want to add only the latest one. I have a regular expression…
BorrajaX
  • 14,201
  • 13
  • 71
  • 117
283
votes
12 answers

Case-insensitive search

I'm trying to get a case-insensitive search with two strings in JavaScript working. Normally it would be like this: var string="Stackoverflow is the BEST"; var result= string.search(/best/i); alert(result); The /i flag would be for…
Chris Boesing
  • 5,159
  • 3
  • 24
  • 30
229
votes
9 answers

How can I do a case insensitive string comparison?

How can I make the line below case insensitive? drUser["Enrolled"] = (enrolledUsers.FindIndex(x => x.Username == (string)drUser["Username"]) != -1); I was given some advice earlier today that suggested I use:…
Jamie
  • 2,307
  • 2
  • 13
  • 3
193
votes
8 answers

String comparison in bash. [[: not found

I am trying to compare strings in bash. I already found an answer on how to do it on stackoverflow. In script I am trying, I am using the code submitted by Adam in the mentioned question: #!/bin/bash string='My string'; if [[ "$string" == *My*…
user1581900
  • 3,290
  • 3
  • 15
  • 21
188
votes
7 answers

How do I compare two strings in Perl?

How do I compare two strings in Perl? I am learning Perl, I had this basic question looked it up here on StackOverflow and found no good answer so I thought I would ask.
PJT
  • 3,220
  • 5
  • 26
  • 38
178
votes
5 answers

How to compare strings ignoring the case

I want apple and Apple comparison to be true. Currently "Apple" == "Apple" # returns TRUE "Apple" == "APPLE" # returns FALSE
Steven
  • 1,803
  • 2
  • 11
  • 4
117
votes
11 answers

Similarity String Comparison in Java

I want to compare several strings to each other, and find the ones that are the most similar. I was wondering if there is any library, method or best practice that would return me which strings are more similar to other strings. For example: "The…
Mario Ortegón
  • 17,860
  • 17
  • 67
  • 79
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,418
  • 10
  • 38
  • 73
1
2 3
99 100