SIGN UP MEMBER LOGIN:    
ARTICLE

Compare Strings in C#

Posted by Mahesh Chand Articles | String in C# August 21, 2010
This code snippet shows how to trim strings in C#.
Reader Level:

Comparing Strings

The Compare method compares two strings and returns an integer value. The return value of Compare method can be less than zero, greater than zero or equals to zero.

Value Meaning
Less than zero When first string is less than second.
Zero When both strings are equal.
Greater than zero When first string is greater than zero.

The following code compares two strings and return results on the System console.

// Comparing two strings
//====================================
string str1 = "ppp";
string str2 = "ccc";
int res = String.Compare(str1, str2);
Console.WriteLine("First result:" +res.ToString());
str2 = "ttt"; res = String.Compare(str1, str2);
Console.WriteLine("Second result:" +res.ToString());
str1 = "ttt"; res = String.Compare(str1, str2);
Console.WriteLine("Third result:" +res.ToString));
//====================================

TheCompareTo method is an instance method. It compares a value (either a string or on object) with a string instance. Return values of this method are same as the Compare method. The following source code compares two strings.

// CompareTo Method
string str = "kkk";
Console.WriteLine( str.CompareTo(str1) );


Read
C# Strings  to learn more about strings in C#.

Login to add your contents and source code to this article
share this article :
post comment
 

string.Compare("W", "V") give the result 1, but string.Compare("WA", "VB") give the result -1

I'm at a computer with Swedish version of Windows, could that impact? Culture Info? The first case seems resonable (W is > V, as we speak strings), but what about case 2?

Regards,
Anders Axi

Posted by Anders Axi Oct 22, 2010
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor