C# में स्ट्रिंग्स की तुलना करने के लिए, तुलना () विधि का उपयोग करें। यह दो स्ट्रिंग्स की तुलना करता है और निम्नलिखित पूर्णांक मान देता है -
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.
दो स्ट्रिंग्स को String.compare() मेथड में सेट करें और उनकी तुलना करें -
string.Compare(string1, string2);
उदाहरण
आप 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();
}
}
} आउटपुट
-1