C# में String Initialize करना आसान है। मान लीजिए आप Name "Amit" Set करना चाहते हैं, तो अपने String को ऐसे Initialize करें।
String str1 = "Hello, World!";
Strings Compare करने के लिए, निम्न C# Method का उपयोग करें।
public static int Compare(string str1, string str2)
Compare करने के लिए, यदि:
String.Compare(str1, str2) == 0
यदि ऊपर 0 के बराबर है, तो दोनों Strings Equal हैं।
ऊपर दिया Method दो Specified String Objects को Compare करता है और एक Integer Return करता है जो Sort Order में उनकी Relative Position को Indicate करता है।
निम्न Example एक String की दूसरे से Comparison दिखाता है।
Example
using System; namespace Demo { class Program { static void Main(string[] args) { string str1 = "John"; string str2 = "Andy"; 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
John and Andy are not equal strings.