String與StringBuilder

來源:互聯網
上載者:User

C#支援兩種字串:規則字串和逐字字串。

我先前看到@+“”這種結構處理字串的時候,也就只是記下怎麼用而已,其實這就是標準的逐字字串,而且分隔字元之間的字元逐字解釋,唯一例外的就是引號。在逐字字串中字串不處理簡單逸出序列及十六進位和Unicode逸出序列,而且可以跨多行。例如:

 

static void Main(string[] args)
        {
            string s = "這個專業\"老師\"太垃圾了!";
            string s1 = @"這個專業""老師""太垃圾了!";
            Console.WriteLine("s-{0}",s1);
            Console.WriteLine("s1-{0}",s1);
            string s2 = @"這個專業
老師
太垃圾了!";
            Console.WriteLine("s2-{0}",s2);
            Console.ReadKey();

String和StringBuilder的不同之處,可以概括為StringBuilder是可變字元序列的類似String類,String我們都知道它是新值新引用,必須分配新的記憶體。這樣當我們多次更改或者串連許多字串的時候可以使用StringBuilder可以提高效能,先分別示範一下String和StringBuilder的一些有代表性的方法和屬性的用法:

using System;

namespace tString
{
    class test
    {
        static void Main()
        {
            //連接字串示範
            string s1 = "Today is ";
            string s2 = "Tuesday!";
            string s = s1 + s2;
            Console.WriteLine(s);
            Console.WriteLine("等效於: string.Concat(s1,s2) s={0}", string.Concat(s1, s2));
            //逐字訪問字串示範
            Console.WriteLine("逐字訪問字串示範\n法1:");
            foreach (char c in s) //法1
            {
                Console.Write(c + " ");
            }
            Console.WriteLine("\n法2");
            char[] str = s.ToCharArray();//將s中的字元複製到unicode字元數組中
            foreach (char r in str)
            {
                Console.Write(r + " ");
            }
            Console.WriteLine("\n法3");
            for (int i = 0; i < s.Length; i++)//索引起始為0,二不是Unicode字元的位置
            {
                Console.Write(s[i]+" ");
            }
            s = s1 + s2;
            //測試字元的搜尋方法
            Console.WriteLine("測試字元的搜尋方法");
            Console.WriteLine("與\"Today\"匹配的第一個字元的索引為{0}", s.IndexOf("Today"));
            string target = "ido";//這裡注意一下,我也是做實驗才發現的,逐字元與target對比
            char[] anyOf = target.ToCharArray();
            int start =0;
            int at;
            at = s.IndexOfAny(anyOf, start);
            if (at > -1)
                Console.WriteLine("s中與\"id\"中匹配的第一個字元的索引為{0}",at);
            else
                Console.WriteLine("(not found)");
            char[] n = { 'i','s'};
            //例如:
            char[] ar = { 'i','s'};
            Console.WriteLine("s中與\"is\"中匹配的第一個字元的索引為{0}", s.IndexOfAny(ar));
            Console.WriteLine("索引0-5對應的子字串為{0}", s.Substring(0, 5));
            //修改字串
            Console.WriteLine("修改字串測試");
            //Insert
            Console.WriteLine("Insert測試");
            Console.WriteLine(s.Insert(17, " What a nice weather!"));
            //PadLeft
            Console.WriteLine("PadLeft測試");
            s = s.PadLeft(s.Length + 8, '♀');
            Console.WriteLine(s);
            //PadRight
            Console.WriteLine("PadRight測試");
            s = s.PadRight(s.Length + 8, '♂');
            Console.WriteLine(s);
            Console.ReadKey();
        }
    }
}

 

 

:由於要限電了只能示範String類了,可以自己研究下StrigBuilder類,和String差不多,幾個典型的方法如Append,AppendFormat,ToString.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.