Compare two string values in CSharp

Description

The following code shows how to compare two string values.

Example


    using System;
/*w  w  w.  j a  v  a  2  s  . c  o m*/
    public class TesterStringManipulationCompare
    {
       public void Run()
       {
           // create some strings to work with
           string s1 = "abcd";
           string s2 = "ABCD";
           int result;  // hold the results of comparisons

           // compare two strings, case sensitive
           result = string.Compare(s1, s2);
           Console.WriteLine(
               "compare s1: {0}, s2: {1}, result: {2}\n",
               s1, s2, result);

           // overloaded compare, takes boolean "ignore case"
           //(true = ignore case)
           result = string.Compare(s1,s2, true);
           Console.WriteLine("Compare insensitive. result: {0}\n",
               result);

       }

       [STAThread]
       static void Main()
       {
          TesterStringManipulationCompare t = new TesterStringManipulationCompare();
          t.Run();
       }
    }

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var