<rp id="wnpn7"><ruby id="wnpn7"></ruby></rp>
<progress id="wnpn7"><track id="wnpn7"><rt id="wnpn7"></rt></track></progress>
<ruby id="wnpn7"></ruby>
<ruby id="wnpn7"><blockquote id="wnpn7"><div id="wnpn7"></div></blockquote></ruby>

    1. <em id="wnpn7"><ruby id="wnpn7"><input id="wnpn7"></input></ruby></em>
      1. <button id="wnpn7"><acronym id="wnpn7"></acronym></button><button id="wnpn7"><acronym id="wnpn7"></acronym></button>

        <rp id="wnpn7"><acronym id="wnpn7"></acronym></rp>

        <li id="wnpn7"><object id="wnpn7"><u id="wnpn7"></u></object></li>
        VB.net 2010 視頻教程 VB.net 2010 視頻教程 python基礎視頻教程
        SQL Server 2008 視頻教程 c#入門經典教程 Visual Basic從門到精通視頻教程
        當前位置:
        首頁 > 編程開發 > c#教程 >
        • C#教程之C#字符串比較方法

        本站最新發布   C#從入門到精通
        試聽地址  
        http://www.squ68.com/eschool/CSharpxin3721/

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

        執行結果如圖:

        相關教程
                
        免费看成年人视频大全_免费看成年人视频在线观看