static
void
Main(
string
[] args)
{
string s = "" ;
// (1)字符訪問(下標訪問s[i])
s = " ABCD " ;
Console.WriteLine(s[ 0 ]); // 輸出"A";
Console.WriteLine(s.Length); // 輸出4
Console.WriteLine();
// (2)打散為字符數組(ToCharArray)
s = " ABCD " ;
char [] arr = s.ToCharArray(); // 把字符串打散成字符數組{'A','B','C','D'}
Console.WriteLine(arr[ 0 ]); // 輸出數組的第一個元素,輸出"A"
Console.WriteLine();
// (3)截取子串(Substring)
s = " ABCD " ;
Console.WriteLine(s.Substring( 1 )); // 從第2位開始(索引從0開始)截取一直到字符串結束,輸出"BCD"
Console.WriteLine(s.Substring( 1 , 2 )); // 從第2位開始截取2位,輸出"BC"
Console.WriteLine();
// (4)匹配索引(IndexOf())
s = " ABCABCD " ;
Console.WriteLine(s.IndexOf( ' A ' )); // 從字符串頭部開始搜索第一個匹配字符A的位置索引,輸出"0"
Console.WriteLine(s.IndexOf( " BCD " )); // 從字符串頭部開始搜索第一個匹配字符串BCD的位置,輸出"4"
Console.WriteLine(s.LastIndexOf( ' C ' )); // 從字符串尾部開始搜索第一個匹配字符C的位置,輸出"5"
Console.WriteLine(s.LastIndexOf( " AB " )); // 從字符串尾部開始搜索第一個匹配字符串BCD的位置,輸出"3"
Console.WriteLine(s.IndexOf( ' E ' )); // 從字符串頭部開始搜索第一個匹配字符串E的位置,沒有匹配輸出"-1";
Console.WriteLine(s.Contains( " ABCD " )); // 判斷字符串中是否存在另一個字符串"ABCD",輸出true
Console.WriteLine();
// (5)大小寫轉換(ToUpper和ToLower)
s = " aBcD " ;
Console.WriteLine(s.ToLower()); // 轉化為小寫,輸出"abcd"
Console.WriteLine(s.ToUpper()); // 轉化為大寫,輸出"ABCD"
Console.WriteLine();
// (6)填充對齊(PadLeft和PadRight)
s = " ABCD " ;
Console.WriteLine(s.PadLeft( 6 , ' _ ' )); // 使用'_'填充字符串左部,使它擴充到6位總長度,輸出"__ABCD"
Console.WriteLine(s.PadRight( 6 , ' _ ' )); // 使用'_'填充字符串右部,使它擴充到6位總長度,輸出"ABCD__"
Console.WriteLine();
// (7)截頭去尾(Trim)
s = " __AB__CD__ " ;
Console.WriteLine(s.Trim( ' _ ' )); // 移除字符串中頭部和尾部的'_'字符,輸出"AB__CD"
Console.WriteLine(s.TrimStart( ' _ ' )); // 移除字符串中頭部的'_'字符,輸出"AB__CD__"
Console.WriteLine(s.TrimEnd( ' _ ' )); // 移除字符串中尾部的'_'字符,輸出"__AB__CD"
Console.WriteLine();
// (8)插入和刪除(Insert和Remove)
s = " ADEF " ;
Console.WriteLine(s.Insert( 1 , " BC " )); // 在字符串的第2位處插入字符串"BC",輸出"ABCDEF"
Console.WriteLine(s);
Console.WriteLine(s.Remove( 1 )); // 從字符串的第2位開始到最后的字符都刪除,輸出"A"
Console.WriteLine(s);
Console.WriteLine(s.Remove( 0 , 2 )); // 從字符串的第1位開始刪除2個字符,輸出"EF"
Console.WriteLine();
// (9)替換字符(串)(Replace)
s = " A_B_C_D " ;
Console.WriteLine(s.Replace( ' _ ' , ' - ' )); // 把字符串中的'_'字符替換為'-',輸出"A-B-C-D"
Console.WriteLine(s.Replace( " _ " , " " )); // 把字符串中的"_"替換為空字符串,輸出"A B C D"
Console.WriteLine();
// (10)分割為字符串數組(Split)——互逆操作:聯合一個字符串靜態方法Join(seperator,arr[])
s = " AA,BB,CC,DD " ;
string [] arr1 = s.Split( ' , ' ); // 以','字符對字符串進行分割,返回字符串數組
Console.WriteLine(arr1[ 0 ]); // 輸出"AA"
Console.WriteLine(arr1[ 1 ]); // 輸出"BB"
Console.WriteLine(arr1[ 2 ]); // 輸出"CC"
Console.WriteLine(arr1[ 3 ]); // 輸出"DD"
Console.WriteLine();
s = " AA--BB--CC--DD " ;
string [] arr2 = s.Replace( " -- " , " - " ).Split( ' - ' ); // 以字符串進行分割的技巧:先把字符串"--"替換為單個字符"-",然后以'-'字符對字符串進行分割,返回字符串數組
Console.WriteLine(arr2[ 0 ]); // 輸出"AA"
Console.WriteLine(arr2[ 1 ]); // 輸出"BB"
Console.WriteLine(arr2[ 2 ]); // 輸出"CC"
Console.WriteLine(arr2[ 3 ]); // 輸出"DD"
Console.WriteLine();
// (11)格式化(靜態方法Format)
Console.WriteLine( string .Format( " {0} + {1} = {2} " , 1 , 2 , 1 + 2 ));
Console.WriteLine( string .Format( " {0} / {1} = {2:0.000} " , 1 , 3 , 1.00 / 3.00 ));
Console.WriteLine( string .Format( " {0:yyyy年MM月dd日} " , DateTime.Now));
// (12)連接成一個字符串(靜態方法Concat、靜態方法Join和實例方法StringBuilder.Append)
s = " A,B,C,D " ;
string [] arr3 = s.Split( ' , ' ); // arr = {"A","B","C","D"}
Console.WriteLine( string .Concat(arr3)); // 將一個字符串數組連接成一個字符串,輸出"ABCD"
Console.WriteLine( string .Join( " , " , arr3)); // 以","作為分割符號將一個字符串數組連接成一個字符串,輸出"A,B,C,D"
StringBuilder sb = new StringBuilder(); // 聲明一個字符串構造器實例
sb.Append( " A " ); // 使用字符串構造器連接字符串能獲得更高的性能
sb.Append( ' B ' );
Console.WriteLine(sb.ToString()); // 輸出"AB"
Console.ReadKey();
}
{
string s = "" ;
// (1)字符訪問(下標訪問s[i])
s = " ABCD " ;
Console.WriteLine(s[ 0 ]); // 輸出"A";
Console.WriteLine(s.Length); // 輸出4
Console.WriteLine();
// (2)打散為字符數組(ToCharArray)
s = " ABCD " ;
char [] arr = s.ToCharArray(); // 把字符串打散成字符數組{'A','B','C','D'}
Console.WriteLine(arr[ 0 ]); // 輸出數組的第一個元素,輸出"A"
Console.WriteLine();
// (3)截取子串(Substring)
s = " ABCD " ;
Console.WriteLine(s.Substring( 1 )); // 從第2位開始(索引從0開始)截取一直到字符串結束,輸出"BCD"
Console.WriteLine(s.Substring( 1 , 2 )); // 從第2位開始截取2位,輸出"BC"
Console.WriteLine();
// (4)匹配索引(IndexOf())
s = " ABCABCD " ;
Console.WriteLine(s.IndexOf( ' A ' )); // 從字符串頭部開始搜索第一個匹配字符A的位置索引,輸出"0"
Console.WriteLine(s.IndexOf( " BCD " )); // 從字符串頭部開始搜索第一個匹配字符串BCD的位置,輸出"4"
Console.WriteLine(s.LastIndexOf( ' C ' )); // 從字符串尾部開始搜索第一個匹配字符C的位置,輸出"5"
Console.WriteLine(s.LastIndexOf( " AB " )); // 從字符串尾部開始搜索第一個匹配字符串BCD的位置,輸出"3"
Console.WriteLine(s.IndexOf( ' E ' )); // 從字符串頭部開始搜索第一個匹配字符串E的位置,沒有匹配輸出"-1";
Console.WriteLine(s.Contains( " ABCD " )); // 判斷字符串中是否存在另一個字符串"ABCD",輸出true
Console.WriteLine();
// (5)大小寫轉換(ToUpper和ToLower)
s = " aBcD " ;
Console.WriteLine(s.ToLower()); // 轉化為小寫,輸出"abcd"
Console.WriteLine(s.ToUpper()); // 轉化為大寫,輸出"ABCD"
Console.WriteLine();
// (6)填充對齊(PadLeft和PadRight)
s = " ABCD " ;
Console.WriteLine(s.PadLeft( 6 , ' _ ' )); // 使用'_'填充字符串左部,使它擴充到6位總長度,輸出"__ABCD"
Console.WriteLine(s.PadRight( 6 , ' _ ' )); // 使用'_'填充字符串右部,使它擴充到6位總長度,輸出"ABCD__"
Console.WriteLine();
// (7)截頭去尾(Trim)
s = " __AB__CD__ " ;
Console.WriteLine(s.Trim( ' _ ' )); // 移除字符串中頭部和尾部的'_'字符,輸出"AB__CD"
Console.WriteLine(s.TrimStart( ' _ ' )); // 移除字符串中頭部的'_'字符,輸出"AB__CD__"
Console.WriteLine(s.TrimEnd( ' _ ' )); // 移除字符串中尾部的'_'字符,輸出"__AB__CD"
Console.WriteLine();
// (8)插入和刪除(Insert和Remove)
s = " ADEF " ;
Console.WriteLine(s.Insert( 1 , " BC " )); // 在字符串的第2位處插入字符串"BC",輸出"ABCDEF"
Console.WriteLine(s);
Console.WriteLine(s.Remove( 1 )); // 從字符串的第2位開始到最后的字符都刪除,輸出"A"
Console.WriteLine(s);
Console.WriteLine(s.Remove( 0 , 2 )); // 從字符串的第1位開始刪除2個字符,輸出"EF"
Console.WriteLine();
// (9)替換字符(串)(Replace)
s = " A_B_C_D " ;
Console.WriteLine(s.Replace( ' _ ' , ' - ' )); // 把字符串中的'_'字符替換為'-',輸出"A-B-C-D"
Console.WriteLine(s.Replace( " _ " , " " )); // 把字符串中的"_"替換為空字符串,輸出"A B C D"
Console.WriteLine();
// (10)分割為字符串數組(Split)——互逆操作:聯合一個字符串靜態方法Join(seperator,arr[])
s = " AA,BB,CC,DD " ;
string [] arr1 = s.Split( ' , ' ); // 以','字符對字符串進行分割,返回字符串數組
Console.WriteLine(arr1[ 0 ]); // 輸出"AA"
Console.WriteLine(arr1[ 1 ]); // 輸出"BB"
Console.WriteLine(arr1[ 2 ]); // 輸出"CC"
Console.WriteLine(arr1[ 3 ]); // 輸出"DD"
Console.WriteLine();
s = " AA--BB--CC--DD " ;
string [] arr2 = s.Replace( " -- " , " - " ).Split( ' - ' ); // 以字符串進行分割的技巧:先把字符串"--"替換為單個字符"-",然后以'-'字符對字符串進行分割,返回字符串數組
Console.WriteLine(arr2[ 0 ]); // 輸出"AA"
Console.WriteLine(arr2[ 1 ]); // 輸出"BB"
Console.WriteLine(arr2[ 2 ]); // 輸出"CC"
Console.WriteLine(arr2[ 3 ]); // 輸出"DD"
Console.WriteLine();
// (11)格式化(靜態方法Format)
Console.WriteLine( string .Format( " {0} + {1} = {2} " , 1 , 2 , 1 + 2 ));
Console.WriteLine( string .Format( " {0} / {1} = {2:0.000} " , 1 , 3 , 1.00 / 3.00 ));
Console.WriteLine( string .Format( " {0:yyyy年MM月dd日} " , DateTime.Now));
// (12)連接成一個字符串(靜態方法Concat、靜態方法Join和實例方法StringBuilder.Append)
s = " A,B,C,D " ;
string [] arr3 = s.Split( ' , ' ); // arr = {"A","B","C","D"}
Console.WriteLine( string .Concat(arr3)); // 將一個字符串數組連接成一個字符串,輸出"ABCD"
Console.WriteLine( string .Join( " , " , arr3)); // 以","作為分割符號將一個字符串數組連接成一個字符串,輸出"A,B,C,D"
StringBuilder sb = new StringBuilder(); // 聲明一個字符串構造器實例
sb.Append( " A " ); // 使用字符串構造器連接字符串能獲得更高的性能
sb.Append( ' B ' );
Console.WriteLine(sb.ToString()); // 輸出"AB"
Console.ReadKey();
}