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

Is Nullable a "Predefined value type" - Or how does Equals() and == work here?

For my own implementation of an Equals() method, I want to check a bunch of internal fields. I do it like this: ... _myNullableInt == obj._myNullableInt && _myString == obj._myString && ... I would assume, that this compares the values, including…
Marcel
  • 15,039
  • 20
  • 92
  • 150
25
votes
3 answers

Is it better to compare strings using toLowerCase or toUpperCase in JavaScript?

I'm going through a code review and I'm curious if it's better to convert strings to upper or lower case in JavaScript when attempting to compare them while ignoring case. Trivial example: var firstString = "I might be A different CASE"; var…
Josh R
  • 1,970
  • 3
  • 27
  • 45
24
votes
5 answers

Bash Shell Scripting - detect the Enter key

I need to compare my input with Enter/Return key... read -n1 key if [ $key == "\n" ] echo "@@@" fi But this is not working.. What is wrong with this code
veda
  • 6,416
  • 15
  • 58
  • 78
23
votes
10 answers

String comparison performance in C#

There are a number of ways to compare strings. Are there performance gains by doing one way over another? I've always opted to compare strings like so: string name = "Bob Wazowski"; if (name.CompareTo("Jill Yearsley") == 0) { //…
Jagd
  • 7,169
  • 22
  • 74
  • 107
23
votes
6 answers

Check if variable starts with 'http'

I'm sure this is a simple solution, just haven't found exactly what I needed. Using php, i have a variable $source. I wanna check if $source starts with 'http'. if ($source starts with 'http') { $source = "
Andelas
  • 2,022
  • 7
  • 36
  • 45
22
votes
7 answers

Check if string is a punctuation character

Let's say I have a String array that contains some letters and punctuation String letter[] = {"a","b","c",".","a"}; In letter[3] we have "." How can I check if a string is a punctuation character? We know that there are many possible punctuation…
sephtian
  • 445
  • 2
  • 11
  • 23
21
votes
4 answers

How to fetch entries starting with the given string from a SQL Server database?

I have a database with a lot of words to be used in a tag system. I have created the necessary code for an autocomplete box, but I am not sure of how to fetch the matching entries from the database in the most efficient way. I know of the LIKE…
Erlend D.
  • 3,013
  • 6
  • 37
  • 59
21
votes
2 answers

Using the less than comparison operator for strings

I'm following a tutorial for C++ and looking at strings and overloading with operators such as +=, ==, != etc. Currently I have a simple if-statement: if(s1 < s2) cout << s2 <
Dmist
  • 345
  • 1
  • 2
  • 4
20
votes
1 answer

Force localeCompare to be case-sensitive

I'm trying to use JavaScript's localeCompare function for sorting strings. I was surprised by the results of running the following lines in the devTools console: "a".localeCompare("b") // returns: -1 "A".localeCompare("b") // returns: -1 Another…
Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115
20
votes
4 answers

Best way to compare 2 urls

I want to compare 2 URLs. Whats the best way to do this? Conditions: 1) It should exclude the http scheme. 2) 'foo.com/a/b' and 'foo.com/a' should be a match.
hegdesachin
  • 285
  • 1
  • 2
  • 9
20
votes
4 answers

String Comparison And Alphabetic Order of Individual Characters

I have a question related to string comparison vs. character comparison. Characters > and 0 (zero) have following decimal values 62 and 48 accordingly. When I compare two characters in the following code, I get value True (which is…
Alexandar
  • 916
  • 2
  • 9
  • 22
19
votes
4 answers

Unexpected result comparing strings with `==`

I have two vectors: a = strsplit("po","")[[1]] [1] "p" "o" b = strsplit("polo","")[[1]] [1] "p" "o" "l" "o" I'm trying to compare them using ==. Unfortunately, a==b gives an unexpected result. a==b [1] TRUE TRUE FALSE TRUE While I expect to…
Maël
  • 45,206
  • 3
  • 29
  • 67
19
votes
2 answers

bash [[ [a] == [a] ]] not true? square bracket affect compare result

Anyone know why this happens? Is this a bug of bash? x='mnt:[4026532411]' [[ $x == $x ]] && echo OK I am expecting result OK, but it did not. Of course, this works [[ "$x" == "$x" ]] && echo OK But as I know, bash [[ ]] have a merit that no need…
osexp2000
  • 2,910
  • 30
  • 29
19
votes
3 answers

is StringComparison.Ordinal the same as InvariantCulture for testing equality?

From their brief summary descriptions, it sounds like the string comparison rules StringComparison.Ordinal and StringComparison.InvariantCulture are meant to differ in how they do sorting of strings. Is that all? i.e., does that mean we can use…
Tim Lovell-Smith
  • 15,310
  • 14
  • 76
  • 93
18
votes
3 answers

Case Insensitive Dictionary with Tuple Key

I have a dictionary where the key is a Tuple where the first item is a Date and the second item is a string. I would like the dictionary to be case insensitive. I know that if the key was just a string I could pass StringComparer.OrdinalIgnoreCase…
Eric
  • 688
  • 2
  • 9
  • 23