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
15
votes
1 answer

JavaScript regex test if string contains specific word (with variable)

I have a regex to check if a string contains a specific word. It works as expected: /\bword\b/.test('a long text with the desired word amongst others'); // true /\bamong\b/.test('a long text with the desired word amongst others'); // false But i…
Alp
  • 29,274
  • 27
  • 120
  • 198
14
votes
6 answers

strcmp or string::compare?

I want to compare two strings. Is it possible with strcmp? (I tried and it does not seem to work). Is string::compare a solution? Other than this, is there a way to compare a string to a char? Thanks for the early comments. I was coding in C++ and…
Perex19
  • 324
  • 1
  • 4
  • 15
14
votes
6 answers

Best way to compare std::strings

What is the best way to compare std::strings? The obvious way would be with if/else: std::string input; std::cin >> input; if ( input == "blahblahblah" ) { // do something. } else if ( input == "blahblah" ) { // do something else. } else…
user542687
14
votes
1 answer

Compare ISO 8601 date strings in javascript

I want to compare ISO 8601 dates in javascript as strings instead of making Date objects for each string and comparing objects. var date_array = ['2012-10-01','2012-11-27','2012-12-23']; console.log(date_array[0] < date_array[1]) // gives…
steampowered
  • 11,809
  • 12
  • 78
  • 98
13
votes
3 answers

Compare output rather than command

Trying to create a script to read a remote file and check the md5 checksum and alert if a mismatch yet getting an error I can't understand. #!/bin/sh REMOTEMD5=$(ssh user@host 'md5sum file.txt') LOCALMD5=$(md5sum 'file.txt') if [$LOCALMD5 !==…
moztech
  • 430
  • 2
  • 4
  • 14
13
votes
6 answers

Caselessly comparing strings in C#

Let's say I have two strings: a and b. To compare whether a and be have the same values when case is ignored, I've always used: // (Assume a and b have been verified not to be null) if (a.ToLower() == b.ToLower()) However, using Reflector, I've…
core
  • 32,451
  • 45
  • 138
  • 193
13
votes
12 answers

equal() and equalsIgnoreCase() return false for equal strings

I'm working with eclipse IDE (Version: 3.4.2) on a mac and I have met the following issue. When comparing between strings using equal() or equalsIgnoreCase() methods I receive false even when the string are equal. For example, the code below…
MByD
  • 135,866
  • 28
  • 264
  • 277
13
votes
9 answers

How do I convert between a measure of similarity and a measure of difference (distance)?

Is there a general way to convert between a measure of similarity and a measure of distance? Consider a similarity measure like the number of 2-grams that two strings have in common. 2-grams('beta', 'delta') = 1 2-grams('apple', 'dappled') = 4 What…
135498
  • 251
  • 1
  • 4
  • 6
13
votes
5 answers

C#: Confusion about ToUpper() and ToLower()

if I do something like this... String myVar = "in"; if(myVar.ToUpper() == "in") { //do something } This is not going to go inside "if" block ..right? or Is it going to check BOTH for "in" AND "IN" and do whatever is there inside that if? If…
Serenity
  • 4,968
  • 19
  • 65
  • 104
13
votes
2 answers

In bash, how to make a comparison and assign the boolean result to a variable

I am doing a string comparison between a variable and a constant. The result of the comparison, either true or false, is assigned to another variable. LABEL=$("${INPUT}" == "flag"); However, I am failing. Any suggestion?
tashuhka
  • 5,028
  • 4
  • 45
  • 64
13
votes
1 answer

Culture-Invariant case-sensitive string comparison returns different results on different machines

I've found that the test results are different on my machine and the build server. I've managed to find the single line that differs. This is a string comparison. The two strings differ in case of the first character. The test below passes on my…
filhit
  • 2,084
  • 1
  • 21
  • 34
13
votes
3 answers

JavaScript: Culture-independent case-insensitive string comparison

If this exact question has been asked before, please point me to the relevant question. tl;dr: How does one compare two strings in JavaScript while ignoring casing according to English rules? My code analyzes and compares data from two different…
BambooleanLogic
  • 7,530
  • 3
  • 30
  • 56
13
votes
8 answers

Why is "F" + "alse" not == "False"?

Possible Duplicate: How do I compare strings in Java? I cant understand why the declared variables is not the same. ex code: String firstPart = "F"; String whole = "False"; String connected = firstPart + "alse"; System.out.println(connected…
user1501127
  • 865
  • 1
  • 18
  • 32
13
votes
10 answers

In C#, what is the best way to compare strings with null and "" return true

I have the following code (as i am trying to detect changes to a field) if (person.State != source.State) { //update my data . . } the issue is I am having cases where person.State is NULL and source.State is "" and thus returning…
leora
  • 188,729
  • 360
  • 878
  • 1,366
12
votes
3 answers

Is there any way to sort strings in all languages?

I have this code. It sorts correctly in French and Russian. I used Locale.US and it seems to be right. Is this solution do right with all languages out there? Does it work with other languages? For example: Chinese, Korean, Japanese... If not, what…
emeraldhieu
  • 9,380
  • 19
  • 81
  • 139