logo

C# Tekenreeks vergelijken()

De C# Compare()-methode wordt gebruikt om de eerste string lexicografisch te vergelijken met de tweede string. Het retourneert een geheel getal.

Als beide strings gelijk zijn, retourneert het 0. Als de eerste string groter is dan de tweede string, retourneert het 1, anders retourneert het -1.

Regel

 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>

Parameters

Eerst: het eerste argument vertegenwoordigt een string die moet worden vergeleken met de tweede string.

seconde: het tweede argument vertegenwoordigt een string die moet worden vergeleken met de eerste string.

Opbrengst

Het retourneert een geheel getal.


Voorbeeld van C# String Compare()-methode

 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)); } } 

Uitgang:

 0 1 -1