字符串函數,其中包括。(1)字符串的大小寫轉換。(2)字符串去收尾空格。(3)字符串分隔符表示
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace String1
- {
- class Program
- {
- static void Main(string[] args)
- {
- /*
- * 字符串的大小寫改變
- string s = "Hello";
- string s1 = s.ToLower();
- string s2 = s1.ToUpper();
- Console.WriteLine("輸出字符串 s :{0}",s);
- Console.WriteLine("將字符串s中的大寫變小寫s1 :{0}",s1);
- Console.WriteLine("將字符串s1中的小寫變大寫s2 : {0}", s2);
- Console.WriteLine("重新查看s 字符串內容是否改變 :{0}", s);
- */
- /*
- *
- * Trim() 函數能夠去掉字符串首位的空格,但是不能去掉中間的空格
- *
- * string s = " faf ";
- string s1 = s.Trim();
- Console.WriteLine("原字符串 s :{0}", s);
- Console.WriteLine("調用Trim()去掉兩邊空白後的字符串 :{0}", s1);
- */
- /*
- * 字符串的比較
- *
- string s = "abc";
- //bool b = "abc".Equals(s);
- bool b = "abc".Equals("ABC", StringComparison.OrdinalIgnoreCase);
- //StringComparison.OrdinalIgnoreCase 忽略大小寫格式
- Console.WriteLine("bool 使用的返回結果 :{0}", b);
- int i = "abc".CompareTo(s);
- int j = "abc".CompareTo("123");
- Console.WriteLine("比較結果i = {0},j = {1}",i,j);
- */
- //string[] Split(param separater)
- //Split 將字符串按照指定的分隔符分隔成字符串數組;
- /*
- string s = "aaa,bb,ccc|dddd,eeee";
- string[] s1 = s.Split(',','|'); //因爲是param 格式,所以可以是多個不同的參數
- foreach (string item in s1)
- Console.WriteLine(item);
- */
- /*
- //處理字符串中的空字符串
- string s2 = "aaa,dd,,dfaf,ddd";
- string[] s3 = s2.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries);
- foreach (string item in s3)
- Console.WriteLine(item);
- */
- string s1 = "我是傑克遜我是麥克斯我是韓庚";
- string[] str = s1.Split(new string[] { "我是" }, StringSplitOptions.RemoveEmptyEntries);
- foreach (string item in str)
- Console.WriteLine(item);
- string s = "2010-10-23";
- string[] s1 = s.Split('-');
- Console.WriteLine("{0}年{1}月{2}日",s1[0],s1[1],s1[2]);
- Console.ReadKey();
- }
- }
- }