學完這個類沒感到它會有什麼用處, 同樣的操作都不如在 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;}