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
70
votes
2 answers

How to compare the contents of two string objects in PowerShell

In PowerShell I have an array of string objects, and I have an object that contains string objects. In Java you can do a .equals(aObject) to test if the string values match, whereas doing a == test if the two objects refer to the same location in…
nullByteMe
  • 6,141
  • 13
  • 62
  • 99
65
votes
3 answers

Compare string similarity

What is the best way to compare two strings to see how similar they are? Examples: My String My String With Extra Words Or My String My Slightly Different String What I am looking for is to determine how similar the first and second string in each…
Brandon
  • 10,744
  • 18
  • 64
  • 97
63
votes
7 answers

Python: Why does ("hello" is "hello") evaluate as True?

Why does "hello" is "hello" produce True in Python? I read the following here: If two string literals are equal, they have been put to same memory location. A string is an immutable entity. No harm can be done. So there is one and only one…
Deniz Dogan
  • 25,711
  • 35
  • 110
  • 162
62
votes
14 answers

Check if one string is a prefix of another

I have two strings which I'd like to compare: String and String:. Is there a library function that would return true when passed these two strings, but false for say String and OtherString? To be precise, I want to know whether one string is a…
fredley
  • 32,953
  • 42
  • 145
  • 236
59
votes
4 answers

What effects does using a binary collation have?

While answering this question, I became uncertain about something that I didn't manage to find a sufficient answer to. What are the practical differences between using the binary utf8_bin and the case insensitive utf8_general_ci collations? I can…
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
55
votes
2 answers

Case insensitive string comparison in Go

How do I compare strings in a case insensitive manner? For example, "Go" and "go" should be considered equal.
user7610
  • 25,267
  • 15
  • 124
  • 150
54
votes
3 answers

Could string comparisons really differ based on culture when the string is guaranteed not to change?

I'm reading encrypted credentials/connection strings from a config file. Resharper tells me, "String.IndexOf(string) is culture-specific here" on this line: if (line.Contains("host=")) { _host = line.Substring(line.IndexOf( "host=") +…
53
votes
6 answers

C++ Compare char array with string

I'm trying to compare a character array against a string like so: const char *var1 = " "; var1 = getenv("myEnvVar"); if(var1 == "dev") { // do stuff } This if statement never validates as true... when I output var1 it is "dev", I was thinking…
Chris Klepeis
  • 9,783
  • 16
  • 83
  • 149
50
votes
5 answers

bash, dash and string comparison

I am trying to compare two strings in a simple shell script. I was using /bin/sh instead of /bin/bash, and after countless hours of debugging, it turns out sh (which is actually dash) can't handle this block of code: if [ "$var" == "string" ] then …
LiraNuna
  • 64,916
  • 15
  • 117
  • 140
47
votes
6 answers

If statement with String comparison fails

I really don't know why the if statement below is not executing: if (s == "/quit") { System.out.println("quitted"); } Below is the whole class. It is probably a really stupid logic problem but I have been pulling my hair out over here not being…
Andrea
45
votes
3 answers

Should I compare a std::string to "string" or "string"s?

Consider this code snippet: bool foo(const std::string& s) { return s == "hello"; // comparing against a const char* literal } bool bar(const std::string& s) { return s == "hello"s; // comparing against a std::string literal } At first…
andreee
  • 4,459
  • 22
  • 42
44
votes
1 answer

What is difference between different string compare methods

Possible Duplicate: Differences in string compare methods in C# In .NET there are many string comparison methods, I just want to confirm which one is the best to use considering…
Pankaj Agarwal
  • 11,191
  • 12
  • 43
  • 59
42
votes
2 answers

Bug in the string comparing of the .NET Framework

It's a requirement for any comparison sort to work that the underlying order operator is transitive and antisymmetric. In .NET, that's not true for some strings: static void CompareBug() { string x = "\u002D\u30A2"; // or just "-ア" if charset…
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
41
votes
9 answers

Comparing a string with the empty string (Java)

I have a question about comparing a string with the empty string in Java. Is there a difference, if I compare a string with the empty string with == or equals? For example: String s1 = "hi"; if (s1 == "") or if (s1.equals("")) I know that one…
user42155
  • 48,965
  • 27
  • 59
  • 60
40
votes
2 answers

Options of the StringComparison Enumeration

I'm confused by the options of the StringComparison Enumeration. I just want to compare two strings ignoring case. Can someone explain what the terms current culture, invariant culture and ordinal mean? Is there an option common to most use cases,…
Dan Stevens
  • 6,392
  • 10
  • 49
  • 68