Compare Strings

  • Thread starter Thread starter Drew
  • Start date Start date
D

Drew

Hello -

I am a converted VB programmer. What I am trying to do
it compare two strings in an if statement. The problem
is that when I use string.compare it always returns a
negative 1. I have entered the same string, different
strings. The string is loaded from the results from a
dataset. The other string is loaded from a textbox.
Could someone please help me in this situation?

Thanks much.
Drew
 
Drew said:
Hello -

I am a converted VB programmer. What I am trying to do
it compare two strings in an if statement. The problem
is that when I use string.compare it always returns a
negative 1. I have entered the same string, different
strings. The string is loaded from the results from a
dataset. The other string is loaded from a textbox.
Could someone please help me in this situation?

Hi Drew,

Your database may contain strings with extra spaces, so you may want to use
Trim(). Here are a few examples of comparing strings:

string str1 = "Some string";
string str2 = "Some string ";

Console.WriteLine(string.Compare(str1, str2)); // -1
Console.WriteLine(string.Compare(str1.Trim(), str2.Trim())); // 0

Console.WriteLine(str1.Equals(str2)); // false
Console.WriteLine(str1 == str2.Trim()); // true


Joe
 
Thanks - I will try right now.

Drew
-----Original Message-----



Hi Drew,

Your database may contain strings with extra spaces, so you may want to use
Trim(). Here are a few examples of comparing strings:

string str1 = "Some string";
string str2 = "Some string ";

Console.WriteLine(string.Compare(str1, str2)); // -1
Console.WriteLine(string.Compare(str1.Trim(), str2.Trim())); // 0

Console.WriteLine(str1.Equals(str2)); // false
Console.WriteLine(str1 == str2.Trim()); // true


Joe
--
http://www.csharp-station.com


.
 
That worked. Thanks for helping an old mind out. For
the life of me I couldn't figure out why that was
happening. Oh - it's the little things that get you.
 
Back
Top