logo

مقارنة سلسلة C # ()

يتم استخدام طريقة C# Compare() لمقارنة السلسلة الأولى بالسلسلة الثانية من الناحية المعجمية. تقوم بإرجاع قيمة عددية.

إذا كانت كلا السلسلتين متساويتين، فإنها ترجع 0. إذا كانت السلسلة الأولى أكبر من السلسلة الثانية، فإنها ترجع 1 وإلا فإنها ترجع -1.

قاعدة

 s1==s2 returns 0 s1&gt;s2 returns 1 s1<s2 returns -1 < pre> <h3>Signatures</h3> <pre> public static int Compare(String first, String second) public static int Compare(String, Int32, String, Int32, Int32) public static int Compare(String, Int32, Int32, String, Int32, Boolean) public static int Compare(String, Boolean, Int32, Int32, String, Int32, CultureInfo) public static int Compare(String, CultureInfo, Int32, Int32, String, Int32, CompareOptions) public static int Compare(String, Int32, Int32, String, Int32, StringComparison) public static int Compare(String, String, Boolean) public static int Compare(String, String, Boolean, CultureInfo) public static int Compare(String, String, CultureInfo, CompareOptions) public static int Compare(String, String, StringComparison) </pre> <h3>Parameters</h3> <p> <strong>first:</strong> first argument represents string which is to be compared with second string.</p> <p> <strong>second:</strong> second argument represents string which is to be compared with first string.</p> <h3>Return</h3> <p>It returns an integer value.</p> <hr> <h2>C# String Compare() Method Example</h2> <pre> using System; public class StringExample { public static void Main(string[] args) { string s1 = &apos;hello&apos;; string s2 = &apos;hello&apos;; string s3 = &apos;csharp&apos;; string s4 = &apos;mello&apos;; Console.WriteLine(string.Compare(s1,s2)); Console.WriteLine(string.Compare(s2,s3)); Console.WriteLine(string.Compare(s3,s4)); } } </pre> <p> <strong>Output:</strong> </p> <pre> 0 1 -1 </pre></s2>

حدود

أولاً: تمثل الوسيطة الأولى السلسلة التي سيتم مقارنتها بالسلسلة الثانية.

ثانية: تمثل الوسيطة الثانية السلسلة التي سيتم مقارنتها بالسلسلة الأولى.

يعود

تقوم بإرجاع قيمة عددية.


مثال على طريقة مقارنة سلسلة C# ().

 using System; public class StringExample { public static void Main(string[] args) { string s1 = &apos;hello&apos;; string s2 = &apos;hello&apos;; string s3 = &apos;csharp&apos;; string s4 = &apos;mello&apos;; Console.WriteLine(string.Compare(s1,s2)); Console.WriteLine(string.Compare(s2,s3)); Console.WriteLine(string.Compare(s3,s4)); } } 

انتاج:

 0 1 -1