469,282 Members | 1,619 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Compare Strings

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
Nov 15 '05 #1
3 2933

"Drew" <an*******@discussions.microsoft.com> wrote in message
news:27****************************@phx.gbl...
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
--
http://www.csharp-station.com
Nov 15 '05 #2
Thanks - I will try right now.

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

"Drew" <an*******@discussions.microsoft.com> wrote in messagenews:27****************************@phx.gbl...
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 useTrim(). 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
.

Nov 15 '05 #3
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.
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by J.W. | last post: by
19 posts views Thread by David zhu | last post: by
14 posts views Thread by Samuel R. Neff | last post: by
11 posts views Thread by balakrishnan.dinesh | last post: by
3 posts views Thread by Twinkle | last post: by
50 posts views Thread by titan nyquist | last post: by
2 posts views Thread by Peter Proost | last post: by
11 posts views Thread by Tony | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.