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

Complex "Contains" string comparison

I'm developing a C# 4.5 app and I need a function to return true for the following comparison: "bla LéOnAr d/o bla".ComplexContains("leonardo") In other words, I need string.Compare(str1, str2, CultureInfo.InvariantCulture,…
Leonardo
  • 10,737
  • 10
  • 62
  • 155
17
votes
10 answers

Ignore case and compare in C#

How to convert the string to uppercase before performing a compare, or is it possible to compare the string by ignoring the case if (Convert.ToString(txt_SecAns.Text.Trim()).ToUpper() == Convert.ToString(hidden_secans.Value).ToUpper())
Murthy
  • 1,502
  • 11
  • 31
  • 45
17
votes
1 answer

comparing two strings in SQL Server

Is there any way to compare two strings in SQL Server 2008 stored procedure like below? int returnval = STRCMP(str1, str2) returns 0 if the strings are the same returns -1 if the first argument is smaller than the second according to the current…
Mahender
  • 5,554
  • 7
  • 38
  • 54
17
votes
13 answers

Checking if 2 strings contain the same characters?

Is there a way to check if two strings contain the same characters. For example, abc, bca -> true aaa, aaa -> true aab, bba -> false abc, def -> false
Brent
  • 181
  • 1
  • 2
  • 5
17
votes
1 answer

cmake string token inclusion check

In cmake, how can I check if a string token is included in another string? In my case, I would like to know if the name of the compiler contains the string "Clang" (e.g. "clang", "AppleClang", ...). All I could do so far…
Pietro
  • 12,086
  • 26
  • 100
  • 193
17
votes
3 answers

Why should I use string.length == 0 over string == "" when checking for empty string in ECMAScript?

Most of the developers on my current project use a (to me) strange way to check for empty strings in ECMAScript: if (theString.length == 0) // string is empty I would normally write this instead: if (theString == "") // string is empty The…
Thomas Müller
  • 15,565
  • 6
  • 41
  • 47
16
votes
2 answers

How do I check if a string contains a certain character?

I'm fairly new to C programming, how would I be able to check that a string contains a certain character for instance, if we had: void main(int argc, char* argv[]){ char checkThisLineForExclamation[20] = "Hi, I'm odd!" int…
Alex
  • 394
  • 1
  • 4
  • 15
16
votes
5 answers

How to find a similar word for a misspelled one in PHP?

I'll explain my problem: I have a database table called country. It has two columns: ID and name. When I want to search for 'paris', but misspelled the word: 'pares' ('e' instead of 'i'), I won't get any result from DB. I want the the system to…
assaqqaf
  • 1,575
  • 4
  • 21
  • 38
16
votes
4 answers

How to compare non english characters with accents

I want to compare 2 strings which have some non English character in them String1 = debarquer String2 = débárquér On comparing above 2 strings, they should say equal.
Mons
  • 161
  • 1
  • 1
  • 3
15
votes
1 answer

Behavior of unique index, varchar column and (blank) spaces

I'm using Microsoft SQL Server 2008 R2 (with latest service pack/patches) and the database collation is SQL_Latin1_General_CP1_CI_AS. The following code: SET ANSI_PADDING ON; GO CREATE TABLE Test ( Code VARCHAR(16) NULL ); CREATE UNIQUE INDEX…
Eric
  • 3,143
  • 2
  • 16
  • 16
15
votes
5 answers

Comparing two different python counter objects

I’m working on an algorithm in Python that would take user input and tell them what new letters they would need to add to a string to make it into a different string, and I’ve been playing around a lot with the dictionaries created by the Counter…
Nick Hart
  • 165
  • 1
  • 1
  • 5
15
votes
4 answers

String.Equals vs String.Compare vs "==" in Action. Explanation needed

Following is the code snippet from a console application - class MyClass { public int GetDay(string data22) { int returnValue = 0; if (string.Compare(data22,"THURSDAY") == 0) // true { returnValue…
Subhasis
  • 714
  • 3
  • 14
  • 28
15
votes
4 answers

How to Compare two strings using a if in a stored procedure in sql server 2008?

I want to do something like this: declare @temp as varchar set @temp='Measure' if(@temp == 'Measure') Select Measure from Measuretable else Select OtherMeasure from Measuretable
Vishal
  • 12,133
  • 17
  • 82
  • 128
15
votes
3 answers

Smart string comparison

I am looking for a library/class that allows smart compare of two strings. At best it would give as a result percent of how two strings are alike. I am comparing company names, addresses that are recordered in different repositories, thus having…
Radoslaw
  • 243
  • 2
  • 8
15
votes
3 answers

Why does string.Compare seem to handle accented characters inconsistently?

If I execute the following statement: string.Compare("mun", "mün", true, CultureInfo.InvariantCulture) The result is '-1', indicating that 'mun' has a lower numeric value than 'mün'. However, if I execute this statement: string.Compare("Muntelier,…
Jonathan
  • 32,202
  • 38
  • 137
  • 208