字符串的属性应用

来源:互联网 发布:whisper是什么软件 编辑:程序博客网 时间:2024/06/04 18:06
//以下是改变字符串中的一个字符的值的方法
 static void Main(string[] args)        {string str = "请输入存放车的品牌,目前有BMW,Benz,Audi,Lambogini:";char[] chs = str.ToCharArray();chs[6] = '之';string s = new string(chs);Console.WriteLine(s);Console.ReadKey(); //请输入存放车之品牌,目前有BMW,Benz,Audi,Lambogini;        } 

查字符串索引位置的相关代码。
string s = "哈哈我又变帅了";int n = s.IndexOf("帅");Console.WriteLine(n);Console.ReadKey();//结果是6,但如果查不到索引位置,返回-1 可以用if else来判断string s = "哈哈我又变帅了帅";int n = s.IndexOf("帅");Console.WriteLine(n);Console.ReadKey();//结果仍是6,说明只返回第一个字符的位置。


字符串分割 Split

static void Main(string[] args)        {string str = "yzk|||||***ml|sk|wcw|zjy|jk";string[]name = str.Split(new char[] { '|','*' }, StringSplitOptions.RemoveEmptyEntries);Console.WriteLine(name); //分割字符串Console.ReadKey();} 

字符串连接 Join

string[] names = { "a", "b", "c", "d" };string str = string.Join("+++", names);Console.WriteLine(str); //a++b++c++dConsole.ReadKey();


字符串的替换
string[] names = { "a", "b", "c", "d", "fcku"};string str = string.Join("+++", names);string newName = str.Replace("fcku", "****");Console.WriteLine(newName); //a++b++c++d++****Console.ReadKey();

还有跟更具体的请参考MSDN。