1. 程式人生 > >C#常用類 [3] String

C#常用類 [3] String

【參考或轉載】第1,2節崎嶇行者張小小Angela,第3節:C# string.Format使用說明

1 string簡介

  字串是一個引用型別,常用的字串操作有:字串定位與子串,字串格式化,字串分割,字串比較,字串連線,字串更改大小寫,字串複製和替換,字串刪除和剪下,字串插入和填充。

2 String常用方法

序號 方法 返回值 說明 舉例
string str = “abcdefabcdef”;
1 IndexOf( char )
IndexOf( string )
IndexOf( char ,int )
IndexOf( string ,int )
int(索引值) 返回第一次出現某字元(串)的索引值,該方法有四種過載 ①str.IndexOf(‘b’) // res:1
②str.IndexOf(“cde”) //res:2
③str.IndexOf(‘f’,2) // res:5
④str.IndexOf(“de”,2) // res:3
2 LastIndexOf(char)
LastIndexOf(string)
int(索引值) 返回最後一次出現某字元(串)的索引值,該方法有兩種過載 ① str.LastIndexOf(‘a’) // res:6
②str.LastIndexOf(“bcd”)// res:7
3 IndexOfAny( char []);
IndexOfAny( char [],int);
int(索引值) 返回字元陣列中任意字元在指定位置開始搜尋第一次出現的位置 ①str.IndexOfAny(new char[]{‘b’,’c’,’d’})// res:1
②IndexOfAny(new char[]{‘b’,’c’,’d’},2) // res:2
4 LastIndexOfAny( char []);
LastIndexOfAny( char [],int);
int(索引值) 返回字元陣列中任意字元在指定位置開始搜尋最後一次出現的位置 ①str.LastIndexOfAny(new char[]{‘b’,’c’,’d’})// res:9
②str.LastIndexOfAny(new char[]{‘b’,’c’,’d’},2) // res:2
5 StartsWith( string ) bool 判斷字串的開頭是否匹配指定的字串 StartsWith( “abc” )// res:true
6 EndsWith( string ) bool 判斷字串的結尾是否匹配指定的字串 EndsWith( “abc” )// res:false
7 Substring(int startIndex )
Substring(int startIndex,int len )
string 擷取字串 ①str.Substring( 2 )// res:"cdefabcdef"
②str.Substring( 2,2 )// res:"cd"
③str.Substring( 2,14 )// res:報錯
8 Insert(int startIndex,string) string 返回一個新的字串,在指定位置插入一個字串 str.Insert(2,“ooo”)//res = “abooocdefabcdef”
9 PadLeft(int startIndex, char) string 若string不足int位,左邊補足char str.PadLeft(15,’*’)// res = “***abcdefabcdef”
10 PadRight(int startIndex, char) string 若string不足int位,右邊補足char str.PadLeft(15,’’)// res = "abcdefabcdef**"
11 Remove(int startIndex)
Remove(int startIndex, int length)
string 從開始位置移除指定個數的字串 ①str.Remove(3)// res ="abc"
②str.Remove(3,6)// res =“abcdef”
12 Replace( char old,char new)
Replace( string old,string new)
string 替換指定的字元或字串 ①str.Replace(‘a’,‘v’)// res ="vbcdefvbcdef"
②str.Replace(“abc”,“v”)// res =“vdefvdef”
13 ToLower() string 將字串轉換成小寫 str.ToLower()// res = “abcdefabcdef”
14 ToUpper() string 將字串轉換成大寫 str.ToUpper()// res = “ABCDEFABCDEF”
15 Trim()
Trim(char [ ])
string 去除頭尾空格/頭尾固定字元 string ss = " &hell o# ";
①ss.Trim() // res = "h&e#ll o"
②ss.Trim( new char[]{’&’,’
’,’#’,’ '})// res = "h
ell o"
16 TrimStart string 去除頭部空格或頭尾特殊字元(同上) string ss = " hello ";
①ss.TrimStart() // res = "hello "
17 TrimEnd string 去除尾部空格或頭尾特殊字元(同上) string ss = " hello “;
ss.TrimEnd() // res = " hello”
18 Split(char)
Split(char [])
string[] 單字元分割字串/多字元分割字串 string ss = “a,bc,def”;
①ss.Split(’,’) // res = {“a”,“bc”,“def”}
② ss.Split(new char[]{’,’,‘e’}) // res = {“a”,“bc”,“d”,“f”}
19 Join( string separator, string[] value )
Join( string separator, string[] value, int startIndex, int count )
string 連線一個字串陣列中的所有元素/或指定長度,使用指定的分隔符分隔每個//指定元素。 string []ss = new string[]{“a”,“bc”,“def”,“a”};
①String.Join("–",ss) // res = “a–bc–def–a"
② String.Join(”–",ss,1,2) // res = “bc–def”
20 Concat( string[] value )
Concat( string str0, string str1 )
Concat( string str0, string str1, string str2 )
string 連線多個 string 物件/或者陣列元素。 string []ss = new string[]{“a”,“bc”,“def”,“a”};
string a0 =“aa”, a1 =“bb”, a2 =“cc”;
①Concat(a0,a1) // res = "aabb"
② Concat(a0,a1,a2) // res = "aabbcc"
③Concat(ss) // res = “abcdefa”

3 string.Former

3.1String.Former 的定義

①String.Format (String, Object,Object, …) 將指定的 String 中的格式項替換為一個/多個指定的 Object 值的文字等效項
② String.Format (String, Object[]) 將指定 String 中的格式項替換為指定陣列中相應 Object 例項的值的文字等效項。

3.2 常用格式化命令

字元 說明 例項 輸出
D/d 十進位制 ①string.Format(“{0:D}”, 9) ;
②string.Format(“{0:D3}”,9);
③string.Format(“{0:d3}”,12345);
①9
②009
③12345
E/e 科學計數法 ①string.Format(“{0:E}”, 12345);
②string.Format(“{0:E3}”, 12345);
①1.234500E+004
②1.235E+004
F/f 小數 ①string.Format(“{0:F}”, 123.678 );
②string.Format(“{0:F3}”,123.678);
①123.67//預設保留兩位
②123.678
G/g 常規 ①string.Format(“{0:G}”, 12345);
②string.Format(“{0:G3}”,12345);
①12345
②1.23E+04
P/p 百分比 ①string.Format(“{0:P}”, 0.12345);
②string.Format(“{0:P3}”,0.12345);
①12.35%// 預設保留兩位小數且擷取時自動四捨五入
②12.345%
X/x 十六進位制 ①string.Format(“{0:X}”, 12);
②string.Format(“{0:X3}”,12);
①C
②00C

3.3 格式化為佔位符形式

①string.Format(“{0:000.00}”,12.056); // 012.06
②string.Format(“{0:000.00}”,12.003);  // 012.00
③string.Format(“{0:000.00}”,12.3);   // 012.30
④string.Format(“{0:###.##}”,12.056); // 12.06
⑤string.Format(“{0:###.##}”,12.003); // 12
⑥string.Format(“{0:###.##}”,12.3);  // 12.3
⑦string.Format(“{0:000.##}”,12.056); // 012.06
零佔位符:如果格式化的值在格式字串中出現“0”的位置有一個數字,則此數字被複制到結果字串中。
數字佔位符:如果格式化的值在格式字串中出現“#”的位置有一個數字,則此數字被複制到結果字串中。否則,結果字串中的此位置不儲存任何值。