學用 ASP.Net 之 “字串” (6): StringInfo 類

來源:互聯網
上載者:User
學完這個類沒感到它會有什麼用處, 同樣的操作都不如在 string 類裡方便, 還要 using System.Globalization;
主要成員:
/* 靜態方法 */StringInfo.GetNextTextElement();       //擷取指定元素, 預設是擷取第一個元素StringInfo.GetTextElementEnumerator(); //擷取列舉程式StringInfo.ParseCombiningCharacters(); //擷取由序號組成的 int[]/* 屬性 */LengthInTextElements; //元素數; 唯讀String;               //對象擁有的字串; 可讀寫/* 對象方法 */SubstringByTextElements(); //截取字串
建構函式及屬性:
protected void Button1_Click(object sender, EventArgs e){    StringInfo sf = new StringInfo("abcdefg");    int n1 = sf.LengthInTextElements; //7    string s1 = sf.String;            //abcdefg    TextBox1.Text = string.Concat(n1, "\n", s1);}protected void Button2_Click(object sender, EventArgs e){    StringInfo sf = new StringInfo();    int n1 = sf.LengthInTextElements; //0    string s1 = sf.String;            //    sf.String = "abc";    int n2 = sf.LengthInTextElements; //3    string s2 = sf.String;            //abc    TextBox1.Text = string.Concat(n1, "\n", s1, "\n" + n2, "\n", s2);}
SubstringByTextElements() 方法:
protected void Button1_Click(object sender, EventArgs e){    StringInfo sf = new StringInfo("ABCDEFG");    string s1 = sf.SubstringByTextElements(2);    //CDEFG    string s2 = sf.SubstringByTextElements(2, 3); //CDE    TextBox1.Text = s1 + "\n" + s2;}
三個靜態方法:
//StringInfo.GetNextTextElement()protected void Button1_Click(object sender, EventArgs e){    string s1 = StringInfo.GetNextTextElement("ABCDEFG");    //A    string s2 = StringInfo.GetNextTextElement("ABCDEFG", 1); //B    TextBox1.Text = s1 + "\n" + s2;}//StringInfo.ParseCombiningCharacters()protected void Button2_Click(object sender, EventArgs e){    int[] nArr = StringInfo.ParseCombiningCharacters("Asp.Net");    TextBox1.Text = string.Join(", ", nArr); //0, 1, 2, 3, 4, 5, 6}//StringInfo.GetTextElementEnumerator(); 使用 IEnumerator 需 using System.Collections;protected void Button3_Click(object sender, EventArgs e){    string str = "ABCDEFG";    string s1, s2;    s1 = s2 = "";    IEnumerator e1 = StringInfo.GetTextElementEnumerator(str);    while (e1.MoveNext())    {        s1 += string.Format("{0} ", e1.Current); //A B C D E F G     }    IEnumerator e2 = StringInfo.GetTextElementEnumerator(str, 2);    while (e2.MoveNext())    {        s2 += string.Format("{0} ", e2.Current); //C D E F G    }    TextBox1.Text = s1 + "\n" + s2;}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.