開篇之作,簡單的對string與string[]進行初步操作,入門篇也,不多說,直接上代碼。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { String[] str1 = new string[]{"A1","A2","A3" }; String str2 = String.Join("," ,str1);//在數組元素之間插入逗號分隔符,返回字符型變量 str2 += ",";//末尾插入逗號分隔符 String[] str3 = str2.Split(',');//使用Split方法將字符型變量轉換成字符數組 Console.WriteLine(str2);//控制台輸出
Console.WriteLine(str3);
Console.WriteLine(str3.Length);//長度是多少?尼瑪,結果是4 ,說明神馬?
Console.ReadKey();
}
}
}
在代碼中添加了個for循環,
for (int i = 0; i < str3.Length; i++)
{
Console.WriteLine(str3[i]);
}
發現原來Console.WriteLine(str3);
這東西不能一次性輸出數組內所有元素的值
分析可知,使用join方法並不會在最后一個元素后面添加逗號,但split方法會將逗號后面的前后元素都識別出來,即使是空字符,看上面第二張圖,與第一張圖進行對比,很明顯發現第二張圖里面光標上面有一個空字符。
以上作為第一篇博客,了以紀念程序猿成長之路的開始。