格式化字符串
//格式化字符串
//一個靜態(tài)的Format方法,,用于將字符串?dāng)?shù)據(jù)格式化成指定的格式
string newstr = String.Format("{0},{1}!!!", str1, str2);
Console.WriteLine(newstr);
Console.ReadLine();
//Format方法將日期格式格式化成指定格式
DateTime dt = DateTime.Now;
string strb = String.Format("{0:D}", dt);
Console.WriteLine(strb);
Console.ReadLine();
截取字符串
//截取字符串
//Substring 該方法可以截取字符串中指定位置的指定長度的字符串
//索引從0開始截取三個字符
string a = "[email protected]";
string b = "";
b = a.Substring(0, 3);
Console.WriteLine(b);
Console.ReadLine();
分割字符串
//分割字符串
//Split 用于分割字符串,,此方法的返回值包含所有分割子字符串的數(shù)組對象
//可以通過數(shù)組取得所有分割的子字符串
string a = "[email protected]";
string[] splitstrings = new string[100];
char[] separator = { '@', '.' };
splitstrings = a.Split(separator);
for(int i = 0; i < splitstrings.Length; i++)
{
Console.WriteLine("item{0}:{1}", i, splitstrings[i]);
}
Console.ReadLine();
插入和填充字符串
//插入和填充字符串
//Insert方法 用于向字符串任意位置插入新元素
//插入
string a = "zhz";
string b;
b = a.Insert(3, "[email protected]");
Console.WriteLine(b);
Console.ReadLine();
//PadLeft/PadRight方法用于填充字符串
//PadLeft方法是在字符串左側(cè)進(jìn)行字符填充
//PadRight方法是在字符串右側(cè)進(jìn)行字符填充
//注意:字符填充?。?!
string a = "";
string b = a.PadLeft(1, 'z');
b = b.PadRight(2, 'h');
Console.WriteLine(b);
Console.ReadLine();
刪除字符串
//刪除字符串
//Remove 方法從一個字符串指定位置開始,,刪除指定數(shù)量的字符
string a = "[email protected]";
string b = a.Remove(3);
string c = a.Remove(3, 3);
Console.WriteLine(b);
Console.WriteLine(c);
Console.ReadLine();
復(fù)制字符串
//復(fù)制字符串
//Copy和CopyTo方法
//Copy用于將字符串或子字符串復(fù)制到另一個字符串或Char類型的數(shù)組中
//CopyTo可以將字符串的某一部分復(fù)制到另一個數(shù)組中
//Copy
string a = "[email protected]";
string b;
b = String.Copy(a);
Console.WriteLine(a);
Console.WriteLine(b);
Console.ReadLine();
//CopyTo
char[] str = new char[100];
//3:需要復(fù)制的字符的起始位置3
//str:目標(biāo)數(shù)組
//0:目標(biāo)數(shù)組中開始存放的位置0
//10:復(fù)制字符的個數(shù)
a.CopyTo(3, str, 0, 10);
Console.WriteLine(str);
Console.ReadLine();
替換字符串
//替換字符串
//Replace方法
//將字符串中的某個字符或字符串替換成其他字符或字符串
string a = "zzzhhhzzz";
string b = a.Replace('z', 'x');
Console.WriteLine(b);
string c = a.Replace("zzzhhhzzz", "fffdddxxx");
Console.WriteLine(c);
Console.ReadLine();
未完待續(xù)。,。,。。,。,。
|