
- 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
Compare two strings lexicographically in C#
To compare strings in C#, use the compare() method. It compares two strings and returns the following integer values −
If str1 is less than str2, it returns -1. If str1 is equal to str2, it returns 0. If str1 is greater than str2, it returns 1.
Set the two strings in the String.compare() method and compare them −
string.Compare(string1, string2);
Example
You can try to run the following code to compare two strings in C#.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo { class MyApplication { static void Main(string[] args) { string string1 = null; string string2 = null; string1 = "amit"; string2 = "Amit"; int myOutput = 0; myOutput = string.Compare(string1, string2); Console.WriteLine(myOutput.ToString()); Console.ReadLine(); } } }
Output
-1
- Related Articles
- Compare two strings lexicographically in Java.
- Compare two Strings lexicographically in Java programming
- Compare the two Strings lexicographically in Java
- Java Program to Compare two strings lexicographically
- C++ Program to Compare two strings lexicographically
- Golang program to compare two strings lexicographically
- Comparing two Strings lexicographically in Java \n
- Compare two Strings in Java
- Program to Compare two strings in Java
- How to compare two strings in Golang?
- Java Program to Compare Two Strings
- Golang program to compare two strings
- How to compare two strings using regex in Python?
- How to compare two strings which are numbers in MySQL?
- How to compare two strings without case sensitive in Java

Advertisements