160 likes | 248 Views
2 、常用字符串操作方法 ( 9 )字符串中字母的大小写转换 利用 ToUpper 可以将字符串的所有英文字母转换为大写,利用 ToLower 可以将字符串的所有英文字母转换为小写。 例如,对前面生成的字符串: Console.WriteLine(str1.ToUpper()); // 结果为 THIS IS A STRING. Console.WriteLine(str1.ToLower()); // 结果为 this is a string. 2 、常用字符串操作方法 ( 10 )从字符串开头或结尾删除指定的字符
E N D
2、常用字符串操作方法 (9)字符串中字母的大小写转换 利用ToUpper可以将字符串的所有英文字母转换为大写,利用ToLower可以将字符串的所有英文字母转换为小写。 例如,对前面生成的字符串: Console.WriteLine(str1.ToUpper()); //结果为 THIS IS A STRING. Console.WriteLine(str1.ToLower()); //结果为this is a string.
2、常用字符串操作方法 (10)从字符串开头或结尾删除指定的字符 可以利用TrimStart删除字符串首部空格,利用TrimEnd删除字符串尾部空格,利用Trim删除字符串首部和尾部空格。例如: string str1=" this is a book."; string str2="this is a book. "; string str3=" this is a book. "; Console.WriteLine(str1.TrimStart()); //删除首部空格 Console.WriteLine(str2.TrimEnd()); //删除尾部空格 Console.WriteLine(str3.Trim()); //删除首尾部空格
2、常用字符串操作方法 (11)填充某个字符到字符串中使总长度等于指定长度 利用PadLeft(总长度,字符)将指定字符重复填充到已有的字符串的左边,使总长度等于指定长度;或者利用PadRight(总长度,字符)将指定字符重复填充到已有的字符串的右边,使总长度等于指定长度。例如: string str="a"; string str1=str.PadLeft(5,'e'); Console.WriteLine(str1); //结果为eeeea string str2=str.PadRight(5,'e'); Console.WriteLine(str2); //结果为aeeee
3.2.3 StringBuilder类 StringBuilder类位于System.Text命名空间下,使用StringBuilder类每次重新生成新字符串时不是再生成一个新实例,而是直接在原来字符串占用的内存空间上进行处理,而且它可以动态地分配占用的内存空间大小。因此,在字符串处理操作比较多的情况下,使用StringBuilder类可以大大提高系统的性能。 默认情况下,编译器会自动为StringBuilder类型的字符串分配一定的内存容量,也可以在程序中直接修改其占用的字节数。 [例3-12] StringBuilder类的使用举例。
using System; using System.Text; namespace StringBuilderExample { class Program { public static void Main() { StringBuilder str=new StringBuilder(); Console.WriteLine("字符串是:“{0}”,长度:{1}",str,str.Length); Console.WriteLine("内存容量分配:{0}",str.Capacity); str=new StringBuilder("test string."); Console.WriteLine("字符串是:“{0}”,长度:{1}",str,str.Length); Console.WriteLine("内存容量分配:{0}",str.Capacity); str.Append("append another string"); Console.WriteLine(“字符串是:“{0}”,长度:{1}",str,str.Length); Console.WriteLine("内存容量分配:{0}",str.Capacity); str=new StringBuilder("test string.",5); Console.WriteLine("字符串是:“{0}”,长度:{1}",str,str.Length); Console.WriteLine("内存容量分配:{0}",str.Capacity); str=new StringBuilder("test string.",40); Console.WriteLine("字符串是:“{0}”,长度:{1}",str,str.Length); Console.WriteLine("内存容量分配:{0}",str.Capacity); Console.ReadLine(); } } }
输出结果: 字符串是: “”,长度:0 内存容量分配:16 字符串是: “test string.”,长度:12 内存容量分配:16 字符串是: “test string. append another string”,长度:33 内存容量分配:33 字符串是: “test string.”,长度:12 内存容量分配:20 字符串是: “test string.”,长度:12 内存容量分配:40 在这个例子中,创建一个新实例时,其长度为0 ,内存分配16个字符的容量,然后又创建了一个长度为12的字符串,内存分配仍然是16个字符的容量,但是在添加了一个字符串使总长度为33(超过16个字符的容量)以后,系统会自动根据新字符串的大小重新为其分配容量。 接着程序指定了一个长度为12的字符串,并为其分配5个字符的空间,显然不能保存12个字符,于是系统就成倍地增加指定的容量,从5变成10,仍然不够,再从10 变为20,结果给长度12的字符串分配了20个字符的空间。 最后程序中又给长度为12的字符串指定了40个字符的空间,系统检测到40比12大,于是就按照指定的空间进行分配。 所以,是否指定字符串的容量,要根据实际情况决定。
3.2.4 DateTime类和TimeSpan类 DateTime类可以表示范围在0001年1月1日午夜12:00:00到9999年12月31日晚上11:59:59之间的日期和时间,最小时间单位等于100纳秒。 TimeSpan类可以表示一个时间间隔。其范围可以在Int64.MinValue到Int64.MaxValue之间。 [例3-13]DateTime类的使用举例。
using System; using System.Text; namespace DateTimeExample { class Program { public static void Main() { DateTime dt1=new System.DateTime(2003,12,31,22,35,5,15); DateTime dt2=new DateTime(2002,7,05); Console.WriteLine("{0:F}---{1}",dt1,dt2); dt1=DateTime.Now; int i=dt1.Day; //当月第几天 int j=dt1.Month; //月 int k=dt1.Year; //年 Console.WriteLine("{0}---{1}---{2}---{3}",dt1,i,j,k);
DateTime t1=dt1.Date; //日期部分 k=dt1.Hour; //小时部分 Console.WriteLine("{0}---{1}",dt1,k); TimeSpan ts1=dt1.TimeOfDay; //当天的时间 TimeSpan ts2=dt1-dt2; i=ts2.Days; Console.WriteLine("{0}---{1}---{2}",ts1,ts2,i); string s1=dt1.ToLongDateString(); string s2=dt1.ToShortDateString(); string s3=string.Format("{0:yyyy.MM.dd}",dt1); //注意:M为月,m为分钟 Console.WriteLine("{0}---{1}---{2}---{3}",dt1,s1,s2,s3); } } }
输出结果: 2003年12月31日 22:35:05---2002-7-5 :00:00 2007-10-25 8:13:25---25---10---2007 2007-10-25 8:13:25---8 08:13:25.2968750---1793.08:13:25.2968750---1793 2007-10-25 8:13:25---2007年10月25日---2007-10-25---2007.10.25 在浏览网页时,经常见到显示当前日期、时间和星期的信息,下面用控制台应用程序说明实现这个功能的方法。 [例13-14]显示当前日期和时间。
using System; using System.Text; namespace DateTimeExample { class Program { public static void Main() { string[] weekDays={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; DateTime now=DateTime.Now; string str=string.Format("{0:现在是yyyy年M月d日,H点m分},{1}",now,weekDays[(int)now.DayOfWeek]); Console.WriteLine(str); DateTime start=new DateTime(2004,1,1); TimeSpan times=now-start; Console.WriteLine("从{0:yyyy年M月d日}起到现在已经过了{1}天!",start,times.Days); } } }
具体时间可能有差异 输出结果: 现在是2007年10月25日,8点25分,星期四 从2004年1月1日起到现在已经过了1393天。
3.2.5 Math类 Math类位于System命名空间下,为三角函数、对数函数和其他通用数学函数提供常数和静态方法。 [例3-15]Math类的使用举例
using System; using System.Text; namespace MathExample { class Program { public static void Main() { int i=10,j=-5; double x=1.3,y=2.7; double a=2.0,b=5.0; Console.WriteLine("-5的绝对值为{0}",Math.Abs(j)); Console.WriteLine("大于等于1.3的最小整数为{0}",Math.Ceiling(x)); Console.WriteLine("小于等于2.7的最大整数为{0}",Math.Floor(y)); Console.WriteLine("10和-5的较大者为{0}",Math.Max(i,j)); Console.WriteLine("1.3和2.7的较小者为{0}",Math.Min(x,y)); Console.WriteLine("2的5次方为{0}",Math.Pow(a,b)); Console.WriteLine("1.3的四舍五入值为{0}",Math.Round(x)); Console.WriteLine("5的平房根为{0}",Math.Sqrt(b)); } } }
输出结果: -5的绝对值为5 大于等于1.3的最小整数为2 小于等于2.7的最大整数为2 10和-5的较大者为10 1.3和2.7的较小者为1.3 2的5次方为32 1.3的四舍五入值为1 5的平房根为2.23606797749979
小 结 介绍了面向对象中string、stringbuilder、Math类的操作与举例。