學用 ASP.Net 之 “字串” (3): string 類的非擴充方法

來源:互聯網
上載者:User
string 類的非擴充成員:
/* 類方法 */string.Compare;            //對比, 返回 1、0 或 -1string.CompareOrdinal;     //對比, 返回序號差string.Concat;             //合并string.Copy;               //複製string.Equals;             //是否同值string.Format;             //格式化輸出string.Intern;             //string.IsInterned;         //string.IsNullOrEmpty;      //是否是 null 或 Emptystring.IsNullOrWhiteSpace; //是否是 null 或 Empty 或空白字元string.Join;               //串連string.ReferenceEquals;    //是否是相同的執行個體, 繼承自 Object/* 變數 */string.Empty; // Null 字元串, 同 ""/* 屬性 */Length;       ///* 對象方法 */Clone;            //引用CompareTo;        //Contains;         //是否包含CopyTo;           //複製部分到字元數組EndsWith;         //尾匹配Equals;           //是否同值GetEnumerator;    //擷取列舉程式GetHashCode;      //GetType;          //GetTypeCode;      //IndexOf;          //尋找IndexOfAny;       //根據字元數組尋找Insert;           //插入IsNormalized;     //LastIndexOf;      //從右邊開始尋找LastIndexOfAny;   //根據字元數組從右邊開始尋找Normalize;        //PadLeft;          //從左邊添加空格或其它字元PadRight;         //從右邊添加空格或其它字元Remove;           //移除Replace;          //替換Split;            //分割StartsWith;       //首匹配Substring;        //截取ToLower;          //轉小寫ToLowerInvariant; //轉小寫, 使用地區大小寫規則ToString;         //ToUpper;          //轉大寫ToUpperInvariant; //轉大寫, 使用地區大小寫規則Trim;             //刪左右空白TrimEnd;          //刪右空白TrimStart;        //刪左空白
大小寫轉換:
protected void Button1_Click(object sender, EventArgs e){    TextBox1.TextMode = TextBoxMode.MultiLine;    string str = "Asp.Net", s1, s2, s3, s4;    s1 = str.ToUpper();          //ASP.NET    s2 = str.ToUpperInvariant(); //ASP.NET    s3 = str.ToLower();          //asp.net    s4 = str.ToLowerInvariant(); //asp.net    TextBox1.Text = string.Concat(s1 + "\n" + s2 + "\n" + s3 + "\n" + s4);}
添加或刪除空白:
protected void Button1_Click(object sender, EventArgs e){    string str, s1, s2, s3, s4, s5, s6, s7, r = "\n";    str = "Asp";    s1 = '>' + str.PadLeft(str.Length + 4) + '    Asp' + str.PadRight(str.Length + 4) + 'Asp    ' + str.PadLeft(7, '*') + '****Asp' + str.PadRight(7, '*') + 'Asp****' + str.TrimStart() + 'Asp    ' + str.TrimEnd() + '    Asp' + str.Trim() + 'Asp
截取:
protected void Button1_Click(object sender, EventArgs e){    string str, s1, s2, r = "\n";    str = "123456789";    s1 = str.Substring(2);    //3456789    s2 = str.Substring(2, 4); //3456    TextBox1.Text = string.Concat(s1 + r + s2);}
分割:
protected void Button1_Click(object sender, EventArgs e){    string str, r = "\n";    str = "1:2-3|4:5-6||7:8-9";    string[] arr1 = str.Split('|');    TextBox1.Text += string.Join(" / ", arr1) + r; //1:2-3 / 4:5-6 /  / 7:8-9 /    string[] arr2 = str.Split('|', ':');    TextBox1.Text += string.Join(" / ", arr2) + r; //1 / 2-3 / 4 / 5-6 /  / 7 / 8-9 /     string[] arr3 = str.Split('|', ':', '-');    TextBox1.Text += string.Join(" / ", arr3) + r; //1 / 2 / 3 / 4 / 5 / 6 /  / 7 / 8 / 9 /     string[] arr4 = str.Split(new char[] { '|', ':', '-' });    TextBox1.Text += string.Join(" / ", arr4) + r; //1 / 2 / 3 / 4 / 5 / 6 /  / 7 / 8 / 9 /     char[] cs = { '|', ':', '-' };    string[] arr5 = str.Split(cs);    TextBox1.Text += string.Join(" / ", arr5) + r; //1 / 2 / 3 / 4 / 5 / 6 /  / 7 / 8 / 9 /     string[] arr6 = str.Split(cs, 3);    TextBox1.Text += string.Join(" / ", arr6) + r; //1 / 2 / 3|4:5-6||7:8-9 /     string[] arr7 = str.Split(cs, StringSplitOptions.RemoveEmptyEntries);    TextBox1.Text += string.Join(" / ", arr7) + r; //1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9 /     string[] arr8 = str.Split(cs, int.MaxValue, StringSplitOptions.RemoveEmptyEntries);    TextBox1.Text += string.Join(" / ", arr8) + r; //1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9 /     string[] arr9 = str.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);    TextBox1.Text += string.Join(" / ", arr9) + r; //1:2-3 / 4:5-6 / 7:8-9 / }
替換:
protected void Button1_Click(object sender, EventArgs e){    string str, r = "\n", s1, s2;    str = "Asp.Net 3.5";    s1 = str.Replace('.', '-');     //Asp-Net 3-5    s2 = str.Replace("3.5", "4.0"); //Asp.Net 4.0    TextBox1.Text = string.Concat(s1, r, s2);}
插入與移除:
protected void Button1_Click(object sender, EventArgs e){    string str, r = "\n", s1, s2, s3;    str = "Asp 3.5";    s1 = str.Insert(3, ".Net"); //Asp.Net 3.5    s2 = str.Remove(3);         //Asp    s3 = str.Remove(0, 4);      //3.5    TextBox1.Text = string.Concat(s1, r, s2, r, s3);}
尋找:
protected void Button1_Click(object sender, EventArgs e){    string str = "Asp.Net 3.5";    int n1, n2, n3, n4, n5, n6, n7;    n1 = str.IndexOf('.');     //3    n2 = str.LastIndexOf('.'); //9    n3 = str.IndexOf('.', 4);  //9    n4 = str.IndexOf("NET");   //-1    n5 = str.IndexOf("NET", StringComparison.CurrentCultureIgnoreCase); //4    n6 = str.IndexOfAny(new char[] {'3', '5'});       //8    n7 = str.LastIndexOfAny(new char[] { '3', '5' }); //10    TextBox1.Text = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}", n1, n2, n3, n4, n5, n6, n7);}
是否包含:
protected void Button1_Click(object sender, EventArgs e){    string str = "Asp.Net 3.5";    bool b1, b2, b3;    b1 = str.Contains('S');   //False    b2 = str.Contains('s');   //True    b3 = str.Contains("Net"); //True    TextBox1.Text = string.Format("{0}\n{1}\n{2}", b1, b2, b3);}
首尾匹配:
protected void Button1_Click(object sender, EventArgs e){    string str = "Asp.Net 3.5";    bool b1, b2, b3, b4;    b1 = str.StartsWith("asp");             //False    b2 = str.StartsWith("asp", StringComparison.CurrentCultureIgnoreCase); //True    b3 = str.StartsWith("asp", true, null); //True    b4 = str.EndsWith("5");                 //True    TextBox1.Text = string.Format("{0}\n{1}\n{2}\n{3}", b1, b2, b3, b4);}
對比:
protected void Button1_Click(object sender, EventArgs e){    string str1 = "1001ABC";    string str2 = "1001abc";    int n1, n2, n3, n4, n5, n6, n7, n8, n9, n0;    n1 = str1.CompareTo(str2); // 1    n2 = str2.CompareTo(str1); //-1    n3 = str1.CompareTo(str1); // 0    n4 = string.Compare(str1, str2);          // 1    n5 = string.Compare(str2, str1);          //-1    n6 = string.Compare(str1, str2, true);    // 0    n7 = string.Compare(str1, 0, str2, 0, 4); // 0    n8 = string.CompareOrdinal(str1, str2);          //-32    n9 = string.CompareOrdinal(str2, str1);          // 32    n0 = string.CompareOrdinal(str1, 0, str2, 0, 4); // 0    TextBox1.Text = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}\n{8}\n{9}", n1, n2, n3, n4, n5, n6, n7, n8, n9, n0);}
是否相同:
protected void Button1_Click(object sender, EventArgs e){    string str1 = "Asp.Net", str2 = "ASP.NET";    bool b1, b2, b3, b4, b5, b6, b7;    b1 = string.Equals(str1, str2);          //False    b2 = string.Equals(str1, str2, StringComparison.CurrentCultureIgnoreCase); //True    b3 = str1.Equals(str2);                  //False    b4 = str1.Equals(str2, StringComparison.CurrentCultureIgnoreCase);         //True    string str3 = str1;    b5 = string.ReferenceEquals(str1, str3); //True    str1 = str1.ToLower();    str2 = str2.ToLower();    b6 = str1 == str2;                       //True    b7 = string.ReferenceEquals(str1, str2); //False    TextBox1.Text = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}", b1, b2, b3, b4, b5, b6, b7);}
複製與引用:
protected void Button1_Click(object sender, EventArgs e){    string str1 = "abc";    string str2 = string.Copy(str1);    string str3 = (string)str1.Clone();    bool b1 = string.ReferenceEquals(str1, str2); //False    bool b2 = string.ReferenceEquals(str1, str3); //True    TextBox1.Text = string.Format("{0}\n{1}", b1, b2);}
複製部分到字元數組:
protected void Button1_Click(object sender, EventArgs e){    char[] cs = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };    string str = "ABCDEFG";    str.CopyTo(1, cs, 3, 2);    TextBox1.Text = string.Join("", cs); // 123BC6789}
是否為空白:
protected void Button1_Click(object sender, EventArgs e){    bool b1, b2, b3, b4, b5;    b1 = string.IsNullOrEmpty(null);         //True    b2 = string.IsNullOrEmpty(string.Empty); //True    b3 = string.IsNullOrEmpty("");           //True    b4 = string.IsNullOrWhiteSpace("   ");   //True    b5 = string.IsNullOrWhiteSpace("123");   //False    TextBox1.Text = string.Format("{0}\n{1}\n{2}\n{3}\n{4}", b1, b2, b3, b4, b5);}
列舉程式:
protected void Button1_Click(object sender, EventArgs e){    // 需 using System.Collections;    string str = "Asp.Net 3.5";    IEnumerator Enum = str.GetEnumerator();    while (Enum.MoveNext())     {        TextBox1.Text += string.Format("{0}|", Enum.Current); //A|s|p|.|N|e|t| |3|.|5|    }}
串連與串聯:
protected void Button1_Click(object sender, EventArgs e){    string str;    str = string.Concat("Asp", '.', "Net", "\x20", 3, '.', 5); //Asp.Net 3.5    TextBox1.Text += str + "\n";    int[] nArr = { 1, 3, 2, 4, 3, 5 };    str = string.Join("*", nArr); //1*3*2*4*3*5    TextBox1.Text += str + "\n";    str = "Asp.Net";    str = string.Join("|", str.ToArray()); //A|s|p|.|N|e|t    TextBox1.Text += str + "\n";    string[] sArr = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" };    str = string.Join("; ", sArr, 1, 3);   //two; three; four    TextBox1.Text += str + "\n"; }
相關文章

聯繫我們

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