Results 1 to 3 of 3

Thread: Comparing String,

  1. #1

    Thread Starter
    Fanatic Member vijy's Avatar
    Join Date
    May 2007
    Location
    India
    Posts
    548

    Comparing String,

    Below two which is the best practise to compare strings...


    Code:
         If "A".ToLower = "a" Then
          '....
         End If
        
       If String.Compare("A", "A", True) = 0 Then
           '...
        End If
    Visual Studio.net 2010
    If this post is useful, rate it


  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Comparing String,

    The first example you provided isn't a true comparison as for example you are changing what the user entered into a lowercase; no longer representing what was entered. A true comparison would be does "A" = "A". So based on that the second example would have to win as it is a comparison IMHO.

  3. #3
    Hyperactive Member guyvdn's Avatar
    Join Date
    Oct 2002
    Location
    Belgium
    Posts
    336

    Re: Comparing String,

    I always use String.Equals as it returns a boolean and you do not have to compare to 0. Additionalty you can define how to compare the values.

    Code:
    If String.Equals("A", "a", StringComparison.OrdinalIgnoreCase) Then
    ...
    End If
    To deny our own impulses is to deny the very thing that makes us human

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width