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
1112
votes
10 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
  • 11,137
  • 3
  • 16
  • 3
694
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
  • 9,469
  • 10
  • 40
  • 53
558
votes
35 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
531
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
  • 11,549
  • 7
  • 23
  • 18
435
votes
14 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…
423
votes
16 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…
Savir
  • 17,568
  • 15
  • 82
  • 136
404
votes
8 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: SELECT * FROM `table` WHERE `column`.contains('{$needle}') In PHP,…
arik
  • 28,170
  • 36
  • 100
  • 156
393
votes
12 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,965
  • 2
  • 15
  • 4
291
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,239
  • 3
  • 26
  • 30
264
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,657
  • 2
  • 16
  • 3
232
votes
12 answers

Good Python modules for fuzzy string comparison?

I'm looking for a Python module that can do simple fuzzy string comparisons. Specifically, I'd like a percentage of how similar the strings are. I know this is potentially subjective so I was hoping to find a library that can do positional…
Soviut
  • 88,194
  • 49
  • 192
  • 260
227
votes
10 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,680
  • 4
  • 18
  • 21
205
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,439
  • 5
  • 29
  • 40
194
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,963
  • 2
  • 11
  • 4
144
votes
12 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
  • 18,670
  • 17
  • 71
  • 81
1
2 3
99 100