
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to use the Compare method of the string class in C#?
The Compare method compares two specified string objects and returns an integer that indicates their relative position in the sort order.
Firstly, set the strings.
string str1 = "Jack"; string str2 = "Mac";
Now compare the strings using the Compare() method and if the comparison results 0, then it would mean the strings are equal.
String.Compare(str1, str2) == 0
Let us see the complete example.
Example
using System; namespace StringApplication { class StringProg { static void Main(string[] args) { string str1 = "Jack"; string str2 = "Mac"; if (String.Compare(str1, str2) == 0) { Console.WriteLine(str1 + " and " + str2 + " are equal strings."); } else { Console.WriteLine(str1 + " and " + str2 + " are not equal strings."); } Console.ReadKey() ; } } }
Output
Jack and Mac are not equal strings.
- Related Articles
- How to use the Clear method of array class in C#?
- How to use the Clone() method of array class in C#?
- How to use the Copy(, ,) method of array class in C#
- How to use the CopyTo(,) method of array class in C#
- How to use the GetLength method of array class in C#?
- How to use the GetLongLength method of array class in C#?
- How to use the GetLowerBound method of array class in C#
- How to use the GetType method of array class in C#?
- How to use the GetUpperBound method of array class in C#?
- How to use the GetValue() method of array class in C#?
- How to use the IndexOf(,) method of array class in C#?
- How to use the Reverse() method of array class in C#
- How to use the SetValue(,) method of array class in C#?
- How to use the Sort() method of array class in C#?
- How to use ReadKey() method of Console class in C#?

Advertisements