C#對字符串進行處理時,經(jīng)常需要進行String,String[]和List
本文分析一下它們的差異和轉換
一.
1. String > String[]
String s = "ab cd ef gh";
String[] sArray = s.Split(' ');2. String[] > String
string[] sArray = {"ab", "cd", "ef", "gh"};string s = String.Join(" ", sArray);//s = "ab cd ef gh"; 3.String[] > List
string[] sArray = { "ab", "cd", "ef", "gh" };
List list = new List(sArray); 4.List
list.Add("ab");
list.Add("cd");
list.Add("ef");
list.Add("gh");
string[] sArray = list.ToArray(); 5.String和List
二.
1. String類型有很多常用的字符串操作成員

字符串是不可變的,雖然這些方法看起來都會改變字符串對象,其實,它們不會改變而是返回了新的
副本。對于一個String,任何“改變”都會分配一個新的恒定字符串。
String s = "ab cd ef gh";
Console.WriteLine("{0}", s.ToUpper());
Console.WriteLine("{0}", s);/*返回結果:
AB CD EF GH
ab cd ef gh*/2. String[]是定長的,String[]一般是在確定字符串數(shù)組的長度的情況下使用
3. List< String >一般在數(shù)組的長度會發(fā)生變化的情況下使用,例如在數(shù)組中間插入一個字符串
網(wǎng)站無須三方授權 · 安全穩(wěn)定、維護方便