Compare two strings with String::Compare : String « Data Type « Visual C++ .NET






Compare two strings with String::Compare

 

#include "stdafx.h"
using namespace System;

int main()
{
   String^ str1 = "cat";
   String^ str2 = "cab";

   if (str1->CompareTo( str2 ) < 0)
   {
       Console::WriteLine(str1 + " is less than " + str2);
   }
      // For variety, use the static method.
   else if ( String::Compare(str1, str2) > 0 )
   {
       Console::WriteLine("{0} is less than {1}", str2, str1);
   }

   else if ( str1->CompareTo( str2 ) == 0)
   {
       Console::WriteLine("The strings are both equal, with value {0}.", str1);
   }
}

   
  








Related examples in the same category

1.String Literals
2.Create a String
3.Create some strings
4.Reverse a string
5.Output a string
6.Insert into a string
7.Remove text from strings
8.Create a copy, then concatenate new text
9.Replace stuff in a concatenated string
10.Use static method String::Compare to compare two strings
11.Compare strings for equality with ==
12.Compare strings for equality with String::Equals
13.Compare strings for equality with String::ReferenceEquals
14.Using string for each loop to output chars
15.String operator plus
16.Convert back to a String using the String constructor that takes a Unicode character array.
17.Convert String to char array
18.Convert String to char pointer
19.Convert text read from stdin to uppercase and write to stdout