Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C# String CompareOrdinal() Method

Posted in C# Tutorial
Updated on Jan 11, 2024
By Mari Selvan
👁ī¸ 103 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
C# String CompareOrdinal() Method

Photo Credit to CodeToFun

🙋 Introduction

In C# programming, strings play a crucial role, and various methods are available to perform string comparisons.

The CompareOrdinal() method is one such method provided by the System.String class. It compares two strings using an ordinal (binary) comparison and returns an integer that indicates their relative position.

In this tutorial, we'll explore the usage and functionality of the CompareOrdinal() method in C#.

💡 Syntax

The syntax for the CompareOrdinal() method is as follows:

Syntax
Copied
Copy To Clipboard
public static int CompareOrdinal(string strA, string strB);

The method is a static member of the System.String class and takes two string parameters (strA and strB). It returns an integer that indicates the relationship between the two strings.

📄 Example

Let's dive into an example to illustrate how the CompareOrdinal() method works.

Program.cs
Copied
Copy To Clipboard
using System;

class Program {
  static void Main() {
    string str1 = "apple";
    string str2 = "banana";

    // Compare the strings using ordinal comparison
    int result = String.CompareOrdinal(str1, str2);

    // Output the result
    Console.WriteLine($"Comparison result: {result}");
  }
}

đŸ’ģ Testing the Program

Output
Comparison result: -1

🧠 How the Program Works

In this example, the CompareOrdinal() method is used to compare two strings, "apple" and "banana," using an ordinal (binary) comparison. The result indicates their relative position.

↩ī¸ Return Value

The CompareOrdinal() method returns an integer that indicates the relationship between the two compared strings. The possible return values are:

  • Less than 0: strA is less than strB.
  • 0: strA is equal to strB.
  • Greater than 0: strA is greater than strB.

📚 Common Use Cases

The CompareOrdinal() method is useful when you need to perform a binary comparison of strings without considering linguistic or cultural differences. It's suitable for scenarios where the byte values of characters are critical.

📝 Notes

  • The CompareOrdinal() method is case-sensitive, meaning that uppercase and lowercase letters are treated as distinct.
  • For culture-aware comparisons that consider linguistic differences, you might want to use the Compare() method with specific CultureInfo settings.

đŸŽĸ Optimization

The CompareOrdinal() method is optimized for performance, as it performs a binary comparison without considering culture-specific rules. Use it when you specifically need ordinal comparison to avoid the overhead associated with culture-aware comparisons.

🎉 Conclusion

The CompareOrdinal() method in C# is a valuable tool for performing binary string comparisons. It provides a straightforward way to compare strings based on their byte values, making it suitable for certain scenarios where cultural differences should not be considered.

Feel free to experiment with different strings and explore the behavior of the CompareOrdinal() method in various cases. Happy coding!

👨‍đŸ’ģ Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
7 months ago

If you have any doubts regarding this article (C# String CompareOrdinal() Method), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy