配色: 字号:
Concat_Join_Split_Insert_PadLeft_PadRigt_Remove
2012-09-28 | 阅:  转:  |  分享 
  


usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

//连接字符串用Concat来处理,它会将参数里的所有数据当成字符串来处理

//在连接字符串的同时会加入分隔号Join,就是在他们之间插入

namespaceStringConcatEg

{

classProgram

{

staticvoidMain(string[]args)

{

/--------------/

stringstr1=string.Concat("\\",''('',''^'',"O",''^'',")","/","~");

//注意其中有字符,单引号括起来的

stringstr2=string.Concat("我发觉你真的是很",2,"啊!");

//可以为其他类型的,但结果会是字符串类型

Console.WriteLine("连接字符串(Concat):");

Console.WriteLine("经过连接后的字符串1为:"+str1);

Console.WriteLine("经过连接后的字符串2为:"+str2);

/--------------/

/--------------/

stringstr3=string.Join("|","王力宏","周杰伦","林俊杰","艾薇儿");

stringstr4=string.Join("~",100,50.24,"Me");

//说明可以为任意类型

stringstr5=string.Join("+-","Eric","Alice");

//作为分隔号也可以为字符串

Console.WriteLine("连接字符串(Join):");

Console.WriteLine("经过连接后的字符串3为:"+str3);

Console.WriteLine("经过连接后的字符串4为:"+str4);

Console.WriteLine("经过连接后的字符串5为:"+str5);

/--------------/

/--------------/

stringstr6="张三love王五love李四love陈六";

string[]arr1=str6.Split(''l'',''o'',''v'',''e'');

//可以按照多个字符进行拆分

stringstr7="刘德华L蔡依林L胡歌";

string[]arr2=str7.Split(''L'');

//可以按照单个字符进行拆分

Console.WriteLine("拆分字符串(Split):");

Console.WriteLine("数量1为:"+arr1.Count());

Console.WriteLine("数量2为:"+arr2.Count());

Console.WriteLine("Str6:");

foreach(varvinarr1)

{

if(!string.IsNullOrEmpty(v.Trim()))//判断不是空字符串的时候打印出来

{

Console.Write(v+"");

}

}

Console.WriteLine("\nStr7:");

foreach(varvinarr2)

{

if(!string.IsNullOrEmpty(v.Trim()))//判断不是空字符串的时候打印出来

{

Console.Write(v+"");

}

}

/--------------/

/--------------/

stringstr8="我爱的她";

stringstr8_Insert=str8.Insert(3,"不是");

//3为要插入的索引值,后面的是要插入的值

Console.WriteLine("\n插入字符串(Insert):");

Console.WriteLine("插入后的字符串为:"+str8_Insert);

/--------------/

/--------------/

stringstr9="鲁木";

stringstr9_PadLeft1=str9.PadLeft(3,''乌'');

//注意3为填充后的总个数后面的参数必须是字符从左边开始填充

stringstr9_PadLeft2=str9.PadLeft(4,''乌'');

//当填充后的总个数超过时,则用后面的参数去填充个数

stringstr9_PadRight1=str9_PadLeft1.PadRight(4,''奇'');

stringstr9_PadRight2=str9_PadLeft1.PadRight(6,''奇'');

//当填充后的总个数超过时,则用后面的参数去填充个数从右边开始填充

Console.WriteLine("填充字符串(PadLeft_PadRight):");

Console.WriteLine("左填充后的字符串1为:"+str9_PadLeft1);

Console.WriteLine("左填充后的字符串2为:"+str9_PadLeft2);

Console.WriteLine("右填充后的字符串1为:"+str9_PadRight1);

Console.WriteLine("右填充后的字符串2为:"+str9_PadRight2);

/--------------/

/--------------/

stringstr10="我飞机哦放假额多想抱着你哭!";

stringstr1_Remove=str10.Remove(1,6);

//1为开始删除的索引值6为删除的个数

Console.WriteLine("删除字符串(Remove):");

Console.WriteLine("删除后的字符串为:"+str1_Remove);

/--------------/

/--------------/

}

}

}



献花(0)
+1
(本文系Honey_Dog首藏)