1

Hello guys i'm trying to create a program in C# where I am comparing two strings in which within the strings they have the double quotation marks. My problem is how do I compare them for equality because it seems the compiler ignores the words within the quotation marks and does not give me the right comparison.

An example is if

string1 = Hi "insert name" here.
string2 = Hi "insert name" here.

I want to use string1.equals(string2). But it seems it tells me the strings are not equal. How do I do this? Please help.

PS. I have no control on what the strings will look like as they are dynamic variables. So I can't just say add an escape sequence to it.

Jacob
  • 77,566
  • 24
  • 149
  • 228
user1215604
  • 11
  • 1
  • 3

2 Answers2

3
string s1 = "Hi \"insert name\" here.";
string s2 = "Hi \"insert name\" here.";

Console.WriteLine((s1 == s2).ToString()); //True

I have no problem ...

Alex
  • 897
  • 10
  • 17
  • Yes I get that you won't have a problem if strings were typed that way. But these strings were inside an excel spreadsheet and were written as "Hi "insert name" here." – user1215604 Feb 17 '12 at 07:16
  • i have no control on what the strings will look like as they are dynamic variables – user1215604 Feb 17 '12 at 07:21
  • When you read from that excel spreadsheet, and assign it to a variable, wouldn't "Hi "insert name" here" become "Hi \"insert name\" here" ? – Tung Feb 17 '12 at 07:45
  • Assuming you are using visual studio, set a breakpoint at a location where the variable is in scope, then open up your immediate window (ctrl + alt + i), and type in the variable's name. You will see that it's representation is most likely as @Alex describes. Viewing the string in the immediate window will also reveal special characters like line feeds and carriage returns. Btw, how are you viewing the string values right now? If you are hovering over the variable and clicking on a magnifying glass icon, then you will **not** see the backslashes used to escape the quotes. – Tung Feb 17 '12 at 23:02
1

.NET will not ignore string values with double quotes when doing comparisons. I think your analysis of what is happening is flawed. For example, given these values:

var string1 = "This contains a \"quoted value\"";
var string2 = "This contains a \"quoted value\"";
var string3 = "This contains a \"different value\"";

string1.Equals(string2) will equal true, and string2.Equals(string3) will equal false.

Here are some potential reasons why you're not seeing an expected result when comparing:

  1. One string may contain different quote characters than another. For example, "this", and “this” are completely different strings.
  2. Your comparison may be failing due to other content not matching. For example, one string may have trailing spaces, and the other may not.
  3. You may be comparing two objects instead of two strings. Object.Equals compares whether two objects are the same object. If you're not dealing with String references, the wrong comparison may be happening.

There are many more potential causes for your issue, but it's not because string comparison ignores double quotes. The more details you provide in your question, the easier it is for us to narrow down what you're seeing.

Jacob
  • 77,566
  • 24
  • 149
  • 228