C#字符串比较

本文详细介绍了在C#中如何进行字符串比较,包括使用str1.Equals(str2)、string.Compare()方法及字符串的CompareTo方法。通过示例代码演示了不同情况下的字符串比较结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,    str1.Equals(str2)

2,    int result = string.Compare(str1,str2);
       int result = string.Compare(str1,str2, true) //忽略大小写比较 

3、在某些语言中,可以利用 >、=、< 来直接比较字符串,而在 C# 中,只能用 == 来比较两个字符串是否相等,更多的比较使用 CompareTo 方法。

语法

public int CompareTo(
    string strB
)
返回值

小于 0,实例小于参数 strB;
0,实例等于参数 strB;
大于 0,实例大于参数 strB,或者 strB 是 null 引用。
示例

string strA = "abc";
string strB = "ABC";
int result = strA.CompareTo(strB);
备注

此方法使用当前区域性执行单词(区分大小写和区域性)比较。

http://blog.csdn.net/CaoJinGangLove/archive/2008/11/05/3227544.aspx

附录:

Console.WriteLine("*******************String.CompareTo()*********************");
            String strTestCompare = "abc";
            //strTestCompare.CompareTo("abc")=0
            //strTestCompare.CompareTo("ab")=1
            //strTestCompare.CompareTo("a")=1
            //strTestCompare.CompareTo("b")=-1
            //String.Compare("abc", "a")=1
            //String.Compare("abc", "ab")=1
            //String.Compare("abc", "abc")=0
            //String.Compare("abc", "b")=-1
            Console.WriteLine(String.Compare("abc", "abc").ToString());
            Console.WriteLine("*******************String.CompareTo()********End**********");
            //String.Compare from MSDN
            //unsafe
            //{
            //    // Null terminated ASCII characters in an sbyte array
            //    String szAsciiUpper = null;
            //    sbyte[] sbArr1 = new sbyte[] { 0x41, 0x42, 0x43, 0x00 };
            //    // Instruct the Garbage Collector not to move the memory
            //    fixed (sbyte* pAsciiUpper = sbArr1)
            //    {
            //        szAsciiUpper = new String(pAsciiUpper);
            //    }
            //    String szAsciiLower = null;
            //    sbyte[] sbArr2 = { 0x61, 0x62, 0x63, 0x00 };
            //    // Instruct the Garbage Collector not to move the memory
            //    fixed (sbyte* pAsciiLower = sbArr2)
            //    {
            //        szAsciiLower = new String(pAsciiLower, 0, sbArr2.Length);
            //    }
            //    // Prints "ABC abc"
            //    Console.WriteLine(szAsciiUpper + " " + szAsciiLower);

            //    // Compare Strings - the result is true
            //    Console.WriteLine("The Strings are equal when capitalized ? " +
            //        (String.Compare(szAsciiUpper.ToUpper(), szAsciiLower.ToUpper()) == 0 ? "true" : "false"));

            //    // This is the effective equivalent of another Compare method, which ignores case
            //    Console.WriteLine("The Strings are equal when capitalized ? " +
            //        (String.Compare(szAsciiUpper, szAsciiLower, true) == 0 ? "true" : "false"));
            //}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值