.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
Sharon Maxwell

  

Compare two string using CompareTo Method of string class

   
 
     


Introduction :

In this tutorial we will learn how to Compare two string using CompareTo Method of string class in .NET Framework using C# and Vb.NET code example

Compare method of string class will return integer value 0 if both given string is matches else it will return 1.

This means 0 when condition is true (in case of matches) 1 in case of false (in case don't macth)

C# Code sample:

using System;

namespace DotNetSparkTutorials
{
    class Program
    {
        static void Main(string[] args)
        {
            string myString = "DotNetSpark";

            string myStringToCompare = "DNS";

            //Output or Result
            Console.WriteLine(myString.CompareTo(myStringToCompare));

            //Wait for input
            Console.ReadLine();
        }
    }
}


Output:

1

VB.NET code example

Namespace DotNetSparkTutorials
	Class Program
		Private Shared Sub Main(args As String())
			Dim myString As String = "DotNetSpark"

			Dim myStringToCompare As String = "DNS"

			'Output or Result
			Console.WriteLine(myString.CompareTo(myStringToCompare))

			'Wait for input
			Console.ReadLine()
		End Sub
	End Class
End Namespace

Output:

1

Other Tutorials in the same Category:
      
  • How to find length or number of characters in a given string      
  • Compare two string using CompareTo Method of string class      
  • Check the word contains in a string or not?      
  • Merge or Concat two string value      
  • Check the string value Equals or not?      
  • Split the string based on delimiter and display or store      
  • Trim or remove white or extra spaces from given string      
  • Get the part or sub string from a given string      
  • Convert string into lowercase of a given string      
  • Convert all string into uppercase of a given string      
  • How to replace string with some other string value?      
  • Remove the specific character or word from a given string      
  • Insert word or characters at any position in a given string or sentence      
  • Display the reverse of the given string      
  • Find string, word or character in a given string      
  • Check the given string is null or empty

  •  
         
     
    Return to Main Index Page


    Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend