String class in VB.NET- String.Compare method

String class represents character strings. you can store values as strings that you can use in an application for a variety of purposes.
  • 3440

Hello all, I am going to start an series of article on String Class in VB.NET. This is the first articles of my series

String: String class represents character strings. you can store values as strings that you can use in an application for a variety of purposes.

The String data type comes from the System.String class . The String type represents a string of Unicode Characters . The String class is a sealed class , so you cannot inherit another class from the String class. You can associate each character with a specified character code, which you can also optionally use to display text. For example, the character "A" is represented by the Unicode character code 0041, or 65 in ASCII.

The String object is Immutable to implementations of String are required to contain a variable-length character buffer positioned a fixed number of bytes after the beginning of the String object.

In this article you will learn about the first Method of String Class that is String.Compare:

The list of Methods which are used with String Class are:

  • String.Compare
  • String.Concat
  • String.Copy
  • String.Clone
  • String.Empty
  • String.Equals
  • String.Format
  • String.GetEnumerator
  • String.GetHashCode()
  • String.IndexOf(String substring)
  • String.IndexOfAny(String sub)
  • String.IndexOfAny(String sub, Int count)
  • String.Insert
  • String.IsNullOrEmpty
  • String.LastIndexOf
  • String.PadLeft(Int length)
  • String.PadLeft(Int length, String char)
  • String.Replace
  • String.Split
  • String.StartsWith
  • String.Substring(Int begin)
  • String.Substring(Int begin, Int end)
  • String.ToCharArray()
  • String.ToLower()
  • String.ToUpper()
  • String.Trim()

Lets start from String.Compare

String.Compare method is handy when you need to compare two string variables. It Compares two specified String objects and returns an integer that indicates their relative position in the sort order. To ignore case, specify True; for the check to include the case, specify False. The returning value can be 1 if the ASCII value of the first string is higher; it can be -1 if the ASCII value of the second string is higher; and it can be 0 if both strings have the same value.

When comparing strings, you should call the Compare method, which requires that you explicitly specify the type of string comparison that the method uses.

Look an Example:

Imports System
Imports System.Collections
 
Public Class compare
    Shared Sub Main()

        Dim string1 As String = "Rahul"
        Dim string2 As String = "RAHUL"
        Dim result As Integer
 
        result = String.Compare(string1, string2)

        Console.WriteLine("Comparision of both String's , result: {2}"_ 
          & Environment.NewLine, string1, string2, result)

        result = String.Compare(string1, string2, True)

        Console.WriteLine("Compare insensitive. result: {0}"_
           & Environment.NewLine, result)

        Console.ReadLine()
    End Sub 
End Class

Output

print.gif

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.