Compare Strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Drew

    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
  • Joe Mayo

    #2
    Re: Compare Strings


    "Drew" <anonymous@disc ussions.microso ft.com> wrote in message
    news:275b01c3a9 60$b49d5430$a60 1280a@phx.gbl.. .[color=blue]
    > 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?[/color]

    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.WriteLi ne(string.Compa re(str1, str2)); // -1
    Console.WriteLi ne(string.Compa re(str1.Trim(), str2.Trim())); // 0

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


    Joe
    --
    Welcome to C# Station!  This is a community site for people interested in applying .NET using the C# programming language.  We’ve been around since July 4th 2000 and have continued to grow over the years.  Items of interest include Articles, Books, Links, Documentation,  and Tutorials. More… Source Code If you would like to see an […]



    Comment

    • Drew

      #3
      Re: Compare Strings

      Thanks - I will try right now.

      Drew[color=blue]
      >-----Original Message-----
      >
      >"Drew" <anonymous@disc ussions.microso ft.com> wrote in[/color]
      message[color=blue]
      >news:275b01c3a 960$b49d5430$a6 01280a@phx.gbl. ..[color=green]
      >> 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?[/color]
      >
      >Hi Drew,
      >
      >Your database may contain strings with extra spaces, so[/color]
      you may want to use[color=blue]
      >Trim(). Here are a few examples of comparing strings:
      >
      > string str1 = "Some string";
      > string str2 = "Some string ";
      >
      > Console.WriteLi ne(string.Compa re(str1, str2)); // -1
      > Console.WriteLi ne(string.Compa re(str1.Trim(),[/color]
      str2.Trim())); // 0[color=blue]
      >
      > Console.WriteLi ne(str1.Equals( str2)); // false
      > Console.WriteLi ne(str1 == str2.Trim()); // true
      >
      >
      >Joe
      >--
      >http://www.csharp-station.com
      >
      >
      >.
      >[/color]

      Comment

      • Drew

        #4
        Re: Compare Strings

        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.

        Comment

        Working...