using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //string类型变量 可以看作只读数组 string myString = "A String"; char myChar=myString[0]; Console.WriteLine("output a char"); Console.WriteLine("{0}", myChar); Console.WriteLine("output string"); foreach(char character in myString) { //Console.WriteLine(myChar); Console.WriteLine("{0}",character); } //对数组进行处理(删除e,s,输入y时,做特殊处理,并输出结果) Console.WriteLine("delete char 'e','s', make a change when click 'y';"); char []trimChars = {' ','e','s'}; string userResponse = Console.ReadLine(); userResponse = userResponse.ToLower(); userResponse = userResponse.Trim(trimChars); if (userResponse == "y") { Console.WriteLine("you click the y"); } Console.WriteLine(userResponse); //对数组进行处理,删除前后空格 Console.WriteLine("delete the space in the beginning and end;"); string userResponse2 = Console.ReadLine(); userResponse2=userResponse2.TrimStart(); userResponse2 = userResponse2.TrimEnd(); Console.WriteLine(userResponse2); //对数组进行处理,添加前后空格 Console.WriteLine("add the space in the beginning;"); string userResponse3 = Console.ReadLine(); userResponse3 = userResponse3.PadLeft(21); Console.WriteLine(userResponse3); //对数组进行处理, 对字符串左边添加'-',使之达到21个字符 //userResponse3 = userResponse3.PadLeft(21,'-'); //对数组进行处理, 对字符串右边添加空格,使之达到21个字符 //userResponse3 = userResponse3.PadRight(21); //对数组进行处理, 根据空格划分,输出每个字符串; Console.WriteLine("output every string ,split by space;"); char[] separator = { ' ' }; string myString3 = "this is a"; string[] myWords; myWords = myString3.Split(separator); foreach(string word in myWords) { Console.WriteLine(word); } //对数组进行处理, 转换大小写,输出字符串长度 string myString4="this is a"; myString4=myString4.ToUpper(); Console.WriteLine(myString4); myString4=myString4.ToLower(); Console.WriteLine(myString4); int i= myString4.Length; Console.WriteLine(i); Console.ReadLine(); } } }
9. C#-- 字符串的一些操作
原创
©著作权归作者所有:来自51CTO博客作者chenhao_asd的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
一些字符串的方法最优方法
简单记录一下字符串的一些最优方法
python string