/* 类方法 */ string.Compare; //对比, 返回 1、0 或 -1 string.CompareOrdinal; //对比, 返回序号差 string.Concat; //合并 string.Copy; //复制 string.Equals; //是否同值 string.Format; //格式化输出 string.Intern; // string.IsInterned; // string.IsNullOrEmpty; //是否是 null 或 Empty string.IsNullOrWhiteSpace; //是否是 null 或 Empty 或空白字符 string.Join; //连接 string.ReferenceEquals; //是否是相同的实例, 继承自 Object /* 变量 */ string.Empty; // 空字符串, 同 "" /* 属性 */ 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< s2 = '>' + str.PadRight(str.Length + 4) + '<'; //>Asp < s3 = '>' + str.PadLeft(7, '*') + '<'; //>****Asp< s4 = '>' + str.PadRight(7, '*') + '<'; //>Asp****< str = " Asp "; s5 = '>' + str.TrimStart() + '<'; //>Asp < s6 = '>' + str.TrimEnd() + '<'; //> Asp< s7 = '>' + str.Trim() + '<'; //>Asp< TextBox1.Text = string.Concat(s1 + r + s2 + r + s3 + r + s4 + r + s5 + r + s6 + r + s7); }
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"; }