By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,590 Members | 1,145 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,590 IT Pros & Developers. It's quick & easy.

Compare Strings

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a

"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

P: n/a
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

P: n/a
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.