c#重新string.方法_String.Compare()方法以及C#中的示例

徐学潞
2023-12-01

c#重新string.方法

C#String.Compare()方法 (C# String.Compare() Method)

String.Compare() method is used to compare two string objects, it returns values 0, less than 0 or greater than 0 based on the difference of first dissimilar characters.

String.Compare()方法用于比较两个字符串对象,它基于第一个不同字符的差返回值0,小于0或大于0。

Syntax:

句法:

    int String.Compare(String1, String2);

Parameter: It accepts two strings to compare.

参数:接受两个字符串进行比较。

Return value: It returns an int value – which may 0, less than 0 or greater than 0.

返回值:返回一个int值-可以为0,小于0或大于0。

Example:

例:

    Input:
    string str1 = "IncludeHelp";
    string str2 = "IncludeHelp";
    
    Function call:
    String.Compare(str1, str2)

    Output:
    0

C#使用String.Compare()方法比较两个字符串的示例 (C# Example to compare two strings using String.Compare() method)

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //string variables
            string str1 = "IncludeHelp";
            string str2 = "IncludeHelp";

            Console.WriteLine(String.Compare("ABCD", "ABCD"));
            Console.WriteLine(String.Compare("ABCD", "abcd"));
            Console.WriteLine(String.Compare("abcd", "ABCD"));

            //checking the condition
            if (String.Compare(str1, str2)==0)
                Console.WriteLine(str1 + " and " + str2 + " are same");
            else
                Console.WriteLine(str1 + " and " + str2 + " are not same");

            str1 = "INCLUDEHELP";
            str2 = "IncludeHelp";

            if (String.Compare(str1, str2) == 0)
                Console.WriteLine(str1 + " and " + str2 + " are same");
            else
                Console.WriteLine(str1 + " and " + str2 + " are not same");

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

输出量

0
1
-1
IncludeHelp and IncludeHelp are same
INCLUDEHELP and IncludeHelp are not same

Reference: String.Compare Method

参考: String.Compare方法

翻译自: https://www.includehelp.com/dot-net/string-compare-method-with-example-in-c-sharp.aspx

c#重新string.方法

 类似资料: