首页 > 代码库 > 字符串及字符的基础应用
字符串及字符的基础应用
字符串的应用 #region 查找字符 //string str = "123456"; //foreach (char time in str) //{ // Console.WriteLine("{0}", time); //} //Console.ReadKey(); #endregion #region 判断字母 //Console.WriteLine("输入字符"); //char c = (char)Console.Read(); //if (Char.IsLetter(c)) //{ // if (char.IsLower(c)) // Console.WriteLine("小写字母?"); // else // Console.WriteLine("小写字母"); //} //else // Console.WriteLine("非字母"); //Console.ReadKey(); #endregion 字符串的位置 string orgstr1 = "123456789"; string orgstr2 = "3456"; int orgstr3 = orgstr1.IndexOf(orgstr2, 0); Console.WriteLine("位置是:" + orgstr3.ToString()); Console.ReadKey(); 结果:2 截取 string orgstr = "123456789"; string name = orgstr.Substring(2, 4); Console.Write(name); Console.ReadKey(); 结果:3456 string orgstr = "123456789"; string name = orgstr.Substring(3); Console.Write(name); Console.ReadKey(); 结果:456789
字符串的插入 string orgstr1 = "123456"; string orgstr2 = "789"; string orgstr3 = orgstr1.Insert(6, orgstr2); Console.WriteLine(orgstr3); Console.ReadKey(); 结果:123456789
字符串的删除 string orgstr1 = "123456789"; string orgstr2 = orgstr1.Remove(3,5); Console.WriteLine(orgstr2); Console.ReadKey(); 结果:1239 如无索引 默认为最后一位 字符串的替换 string orgstr1 = "123456"; string orgstr2 = orgstr1.Replace("3", "9"); Console.WriteLine(orgstr2); Console.ReadKey(); 结果:129456
字符的分段 string orastr1 = "a|bb|ccc"; char[] arrag = new char[] { ‘|‘ }; string[] orastr2 = orastr1.Split(arrag, 3); for (int i = 0; i < orastr2.Length; i++) { Console.WriteLine(orastr2[i]); } Console.ReadKey(); 结果:a bb ccc
输出字符 string str = "123456"; foreach (char time in str) { Console.WriteLine("{0}", time); } Console.ReadKey();