char[] cArray = new char[10];
cArray[0] = 'a';
cArray[1] = 'b';
cArray[2] = 'c';
cArray[3] = 'd';
cArray[4] = 'e';
string temp;
//知道字符串的长度,如果是从文件里读取的char[],长度是知道的
temp = new string(cArray, 0, 5);
Response.Write("|" + temp + "|" + temp.Length.ToString() + "<br />");
//如果不知道字符串的长度,直接转成字符串后,长度是字符数组的长度
temp = new string(cArray);
Response.Write("|" + temp + "|" + temp.Length.ToString() + "<br />");
//可以循环每一个字符,只加入非null值,这样就可以去掉字符串里的null值了
StringBuilder sb = new StringBuilder("");
foreach (char c in temp) //foreach (char c in cArray),字符串就是字符数组
{
if (Convert.ToInt32(c) != 0)
{
sb.Append(c);
}
}
temp = sb.ToString();
Response.Write("|" + temp + "|" + temp.Length.ToString() + "<br />");