C#教程之C#字符串比较方法|C#教程

金沙澳门官网网址:www.js333com

VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • C#教程之C#字符串比较方法

本站最新发布   C#从入门到精通
试听地址  
/eschool/CSharp奥门金沙网址/

String 类主要包括两种类型的字符串比较方法:Compare 和 CompareTo

Compare(String, String) 比较两个字符串, 并且返回一个integer来表明比较关系。

public static int Compare (string strA, string strB);

如果返回值小于0, strA 小于strB.

如果返回值等于0, strB 和strA相等.

如果返回值大于0, strA 大于strB.

string strA1 = "abb";
string strB1 = "abc";
Console.WriteLine(String.Compare(strA1, strB1)); //return -1
string strA2 = "1235";
string strB2 = "1234";
Console.WriteLine(String.Compare(strA2, strB2)); //return 1

CompareTo(String) 和compare有相同的判断逻辑,唯一不同的是compare是静态版本。

public int CompareTo (string strB);

下面是一个判断各种类型compare结果的例子:

using System;
class Sample
{
    public static void Main()
    {
        DateTime now = DateTime.Now;
        // Time span = 11 days, 22 hours, 33 minutes, 44 seconds
        TimeSpan tsX = new TimeSpan(11, 22, 33, 44);
        // Version = 1.2.333.4
        Version versX = new Version("1.2.333.4");
        // Guid = CA761232-ED42-11CE-BACD-00AA0057B223
        Guid guidX = new Guid("{CA761232-ED42-11CE-BACD-00AA0057B223}");
        Boolean a1 = true, a2 = true;
        Byte b1 = 1, b2 = 1;
        Int16 c1 = -2, c2 = 2;
        Int32 d1 = 3, d2 = 3;
        Int64 e1 = 4, e2 = -4;
        Decimal f1 = -5.5m, f2 = 5.5m;
        Single g1 = 6.6f, g2 = 6.6f;
        Double h1 = 7.7d, h2 = -7.7d;
        Char i1 = 'A', i2 = 'A';
        String j1 = "abc", j2 = "abc";
        DateTime k1 = now, k2 = now;
        TimeSpan l1 = tsX, l2 = tsX;
        Version m1 = versX, m2 = new Version("2.0");
        Guid n1 = guidX, n2 = guidX;
        try
        {
            Show("Boolean:  ", a1, a2, a1.CompareTo(a2));
            Show("Byte:     ", b1, b2, b1.CompareTo(b2));
            Show("Int16:    ", c1, c2, c1.CompareTo(c2));
            Show("Int32:    ", d1, d2, d1.CompareTo(d2));
            Show("Int64:    ", e1, e2, e1.CompareTo(e2));
            Show("Decimal:  ", f1, f2, f1.CompareTo(f2));
            Show("Single:   ", g1, g2, g1.CompareTo(g2));
            Show("Double:   ", h1, h2, h1.CompareTo(h2));
            Show("Char:     ", i1, i2, i1.CompareTo(i2));
            Show("String:   ", j1, j2, j1.CompareTo(j2));
            Show("DateTime: ", k1, k2, k1.CompareTo(k2));
            Show("TimeSpan: ", l1, l2, l1.CompareTo(l2));
            Show("Version:  ", m1, m2, m1.CompareTo(m2));
            Show("Guid:     ", n1, n2, n1.CompareTo(n2));
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        Console.ReadLine();
    }
    public static void Show(string caption, Object var1, Object var2,
                            int resultGeneric)
    {
        string relation;
        Console.Write(caption);
        if (resultGeneric < 0) relation = "less than";
        else if (resultGeneric > 0) relation = "greater than";
        else relation = "equal to";
        Console.WriteLine("{0} is {1} {2}", var1, relation, var2);
    }
}

执行结果如图:

相关教程
        
Baidu
sogou