Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Compare two strings - get culture – ordinal - invariant culture (ignoring case) of string comparer using string comparer class vb.net and C# .net.

Author: Maliha Atteeq
Download Source Code : 1009_string comparer.zip

In this simple article you will learn that how to compare two strings, get culture, ordinal, invariant culture(ignoring case) of string comparer using string comparer class in vb and C# .net.

 Compare two strings:

In order to compare two strings, string comparer class provides us a function named as compare(). It is an overloaded function. I will discuss the most common and used one. The function takes two arguments as parameters which are of type string. The return type of this function is integer. By analyzing the returned integer we can compare the two strings.

To demonstrate make a new window application. Drag one button and two text boxes on from.

Now write the following code on Button click event:

C#:

private void btn_compare_Click(object sender, EventArgs e)

        {

            String str1;

            String str2;

            int temp;

            str1 = txt_str1.Text;

            str2 = txt_str2.Text;

            StringComparer ob;

            ob = StringComparer.CurrentCulture;

            temp=ob.Compare(str1, str2);

            if (temp < 0)

            {

                MessageBox.Show("first string is less than the second string");

            }

            else if (temp > 0)

            {

                MessageBox.Show("first string is greater than the second string");

            }

            else if (temp == 0)

            {

                MessageBox.Show("both strings are equal");

            }

        }

VB:

Private Sub btn_compare_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim str1 As [String]

        Dim str2 As [String]

        Dim temp As Integer

        str1 = txt_str1.Text

        str2 = txt_str2.Text

        Dim ob As StringComparer

        ob = StringComparer.CurrentCulture

        temp = ob.Compare(str1, str2)

        If temp < 0 Then

            MessageBox.Show("first string is less than the second string")

        ElseIf temp > 0 Then

            MessageBox.Show("first string is greater than the second string")

        ElseIf temp = 0 Then

            MessageBox.Show("both strings are equal")

        End If

    End Sub

Get current culture:

In order to get the current culture of the string comparer, string comparer class provides us a property named as currentculture. This property returns the culture and the return type of this property is string comparer that is it returns the object of string comparer class that is used to perform case insensitive comparison.

To demonstrate make a new window application. Drag one button on from.

Now write the following code on Button click event:

C#:

private void btn_getCulture_Click(object sender, EventArgs e)

        {

            StringComparer ob;

            ob = StringComparer.CurrentCultureIgnoreCase;

            MessageBox.Show("the current culture ignoring case is "+Convert.ToString(ob));

        }

 

VB:

Private Sub btn_getCulture_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim ob As StringComparer

        ob = StringComparer.CurrentCultureIgnoreCase

        MessageBox.Show("the current culture ignoring case is " & Convert.ToString(ob))

    End Sub

 

 

Get Ordinal:

In order to perform the case sensitive ordinal string comparison, string comparer class provides us a property named as ordinal. This property returns the ordinal and the return type of this property is string comparer that is it returns the object of string comparer class that is used to perform case insensitive comparison.

To demonstrate make a new window application. Drag one button on from.

Now write the following code on Button click event:

C#:

private void btn_getOrd_Click(object sender, EventArgs e)

        {

            StringComparer ob;

            ob = StringComparer.OrdinalIgnoreCase;

            MessageBox.Show("the ordinal ignoring case is " + Convert.ToString(ob));

        }

 

VB:

Private Sub btn_getOrd_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim ob As StringComparer

        ob = StringComparer.OrdinalIgnoreCase

        MessageBox.Show("the ordinal ignoring case is " & Convert.ToString(ob))

    End Sub

Get invariant culture:

In order toperform the case sensitive string comparison by using the word comparison rule of the invariant culture, string comparer class provides us a property named as InvariantCulture. This property returns the invariant culture and the return type of this property is string comparer that is it returns the object of string comparer class that is used to perform case insensitive comparison.

To demonstrate make a new window application. Drag one button on from.

Now write the following code on Button click event:

C#:

private void btn_getInvarCulture_Click(object sender, EventArgs e)

        {

            StringComparer ob;

            ob = StringComparer.InvariantCultureIgnoreCase;

            MessageBox.Show("the invariant culture ignoring case is " + Convert.ToString(ob));

 

        }

VB:

Private Sub btn_getInvarCulture_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim ob As StringComparer

        ob = StringComparer.InvariantCultureIgnoreCase

 

        MessageBox.Show("the invariant culture ignoring case is " & Convert.ToString(ob))

    End Sub

Now write the following code on FORM LOAD event:

C#:

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP STRING COMPARER APPLICATION";

        }

 

Vb:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = "DEVASP STRING COMPARER APPLICATION"

    End Sub

This simple article tells that how to compare two strings, get culture, ordinal, invariant culture(ignoring case) of string comparer using string comparer class in vb and C# .net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Use – initialize - get the platform – version – versionString - servicePack of operating system using operating system class vb.net - C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net