第3章常用数据类型的用法.ppt
《第3章常用数据类型的用法.ppt》由会员分享,可在线阅读,更多相关《第3章常用数据类型的用法.ppt(55页珍藏版)》请在三一文库上搜索。
1、第3章 常用数据类型的用法,3.1 字符串 3.2 数组 3.3 枚举 3.4 泛型 3.5 泛型集合 3.6 日期与时间处理 3.7 数学运算 3.8 随机数,3.1 字符串,字符串是由一个或多个Unicode字符构成的一组字符序列。 1.字符串的创建与表示形式 String类(string) : string myString = “some text“; string s2 = new string(a,2); /结果为aa String.Empty 表示空字符串 String类的表示方法: string str = “C:testfirst.cs“ string str = “C:te
2、stfirst.cs“ 取字符串中的某个字符的方法: char chFirst = myString2; /结果为m,3.1 字符串,2. 字符串比较 字符串比较方法 string.Compare(string s1,string s2) 如果s1大于s2,结果为1; 如果s1等于s2,结果为0; 如果s1小于s2,结果为-1。 string.Compare(string s1,string s2,bool ignoreCase) ignoreCase确定是否区分大小写 比较两个字符串是否相等,使用: Equals方法 =,3.1 字符串,3. 字符串查找 Contains方法:用于查找一个字
3、符串中是否包含指定的子字符串。语法为: public bool Contains( string value ) 例如:string str1 = “this is a string.“; bool b=str1.Contains(“is“ ); IndexOf方法:求某个字符或者子串在字符串中出现的位置。两种最常用的重载形式: public int IndexOf(string s ) public int IndexOf(string s, int startIndex ) LastIndexOf方法:LastIndexOf方法的用法与IndexOf方法相同,查找最后一次出现的位置。,3.
4、1 字符串,例如: Console.WriteLine(str1.IndexOf(“is“); /结果为2(注意序号从0开始) Console.WriteLine(str1.LastIndexOf(“is“); /结果为5 IndexOfAny方法:查找某个字符串中是否包含某些字符(多个不同的字符)。 常用语法:public int IndexOfAny( char anyOf ) 例如: string s1 = “123abc123abc123“; char c = a, b, 5, 8 ; int x = s1.IndexOfAny(c); /x结果为3,3.1 字符串,4. 求子字符串
5、Substring(int startIndex) 截取从startIndex开始到字符串结尾的字符串。 Substring(int startIndex,int count) 截取字符串从startIndex位开始,截取长度为count的字符串。 例如: string str=“abc123”; string s1=str.Substring(2); /s1: c123 string ss = str.Substring(1,4); Console.WriteLine(ss);/bc12,3.1 字符串,5. 字符串的插入、删除和替换 插入字符串 语法形式: public string In
6、sert(int startIndex,string value) 含义:从startIndex开始插入子字符串value 例如: string s1 = “abcdabcd“; string s2 = s1.Insert(2, “12“);/结果为“ab12cdabcd “,3.1 字符串,删除字符串 两种常用形式: 1)Remove(int startIndex) 删除从startIndex到字符串结尾的子字符串 2)Remove(int startIndex,int count) 删除从startIndex开始的count个字 例如: string str1 = “this is a s
7、tring.” string s1=str1.Remove(2);/ s1结果为th Console.WriteLine(str1.Remove(1,2); /结果为ts is a string,3.1 字符串,字符串替换 两种常用形式: 1)Replace (string oldStr,string newStr) 2)Replace(char oldChar,char newChar) 例如: string str1 = “this is a string.” string s1=str1.Replace(a,b); /结果为this is b string. Console.WriteL
8、ine(str1.Replace(“is“,“xy“); /结果为thxy xy a string,3.1 字符串,6. 移除首尾指定的字符 从字符串开头或结尾删除指定的字符 :利用TrimStart删除字符串首部字符,利用TrimEnd删除字符串尾部字符,利用Trim删除字符串首部和尾部字符。默认移除空格。 例如:string s1 = “this is a book“; string s2 = “that is a pen“; string s3 = “is a pen “; Console.WriteLine(s1.TrimStart(); /删除首部空格 Console.WriteLi
9、ne(s2.TrimEnd(); /删除尾部空格 Console.WriteLine(s3.Trim(); /删除首部和尾部空格,3.1 字符串,7. 字符串的合并与拆分 Join方法:在数组的每个元素之间串联指定的分隔符,从而产生单个串联的字符串。 语法为: public static string Join( string separator, string value ) Split方法:将字符串按照指定的一个或多个字符进行分离,从而得到一个字符串数组。 常用语法为: public string Split( params char separator ),字符串的合并和拆分举例,例如:
10、 string sArray1 = “123“, “456“, “abc“ ; string s1 = string.Join(“,“, sArray1); /结果为“123,456,abc“ string sArray2 = s1.Split(,); /sArray2得到的结果与sArray1相同 string s2 = “abc 12;34,56“; string sArray3 = s2.Split(, ;, ); /分隔符为逗号、分号、空格 Console.WriteLine(string.Join(Environment.NewLine,sArray3); 输出结果: abc 12
11、34 56,3.1 字符串,8. 字符串中字母的大小写转换 ToUpper大写 ToLower小写 例如: string s1 = “This is a string“; string s2 = s1.ToUpper( ); /s2结果为THIS IS A STRING string s3 = Console.ReadLine( ); if (s3.ToLower( ) = “yes“) Console.WriteLine(“OK“); ,9. String和StringBuilder,String类实际上表示的是一系列不可变的字符。说其实例是“不可变的”,是因为无法直接修改给该字符串分配的堆
12、中的字符串。例如,在myString的后面接上另一个字符串: myString += “ and a bit more“; 其实际操作并不是在原来myString所占内存空间的后面直接附加上第二个字符串,而是返回一个新String实例,即重新为新字符串分配内存空间。 StringBuilder类位于System.Text名称空间下,使用StringBuilder类每次重新生成新字符串时不是再生成一个新实例,而是直接在原来字符串占用的内存空间上进行处理,而且它可以动态的分配占用的内存空间大小。,StringBuilder类用法举例,【例3-1】StringBuilder类的基本用法。 using
13、 System; using System.Text; namespace StringBuilderExample class Program public static void Main( ) StringBuilder sb = new StringBuilder( ); sb.Append(“string1“); sb.AppendLine(“string2“); sb.Append(“string3“); string s = sb.ToString( ); Console.WriteLine(s); Console.ReadLine( ); ,StringBuilder类用法总结
14、,常用方法 Append:追加字符串 AppendFormat:追加格式化字符串 Remove(int startIndex,int length):从StartIndex位置开始移除长度为length的字符串 ToString() 举例: StringBuilder builder = new StringBuilder(); builder.Append(“abc123”); builder.AppendFormat(“0”,“456”); builder.Remove(1,2); builder.ToString();/ a123456,StringBuilder适用场合,StringB
15、uilder类适用于字符串连接操作比较多的情况下,它可以大大提高系统的性能。 性能测试对比:拼接从0开始的数字字符串,观察所用的时间,3.2 数组,数组用于存储同一种类型的数据。 数组是一种引用类型,而不是值类型。 数组是按照数组名、数据元素的类型和维数来描述的。 C#中可以有一维数组、多维数组、交错数组。 1.数组的声明与初始化 声明数组时,既可以一开始就指定数组元素的个数,也可以一开始不指定元素个数,而是在使用数组元素前动态地指定元素个数。 例如: int a = new int30; string mystring = “first“,“second“,“third“; 注意:不带new
16、运算符的简化形式只能在声明语句中。,数组的声明语法举例,数组的秩与数组长度,2.数组的秩与数组长度 数组的秩(rank)是指数组的维数,例如一维数组秩为1,二维数组秩为2。 数组长度是指数组中所有元素的个数。例如: int a = new int10; /数组长度为10 int, b = new int3, 5; /数组长度为3*5=15,其中第0维长度为3,第1维长度为5 Array类提供访问秩和长度的属性和方法。 (1)Rank属性 (2)Length属性 (3)GetLength(i) 获取数组某一维的长度。,数组用法举例,【例3-2】编写一个控制台应用程序,定义一个二维数组,分别输出数
17、组的秩、数组长度以及数组中的每个元素值。,数组,3.一维数组 在C#中,数组的下标默认是从0开始索引的。 Length属性表示数组的长度。 4.多维数组 多维数组指维数大于1的数组,常用的是二维数组和三维数组。 C#语言支持两种类型的二维数组,一种是二维矩形数组,另一种是二维交错数组。 对于二维矩形数组,数组中的每一行都有相同的列数。 例如: int, n2 = new int, 1, 2, 3, 4, 5, 6 ;,数组,5.交错数组 交错数组相当于一维数组的每一个元素又是一个数组,也可以把交错数组称为“数组的数组”。 例如: int n1 = new int2 new int 2,4,6,
18、 new int 1,3,5,7,9 ; 交错数组的每一个元素既可以是一维数组,也可以是多维数组。,交错数组用法举例,【例3-3】交错数组的用法。,数组,6.动态改变数组的大小 Resize方法 public static void Resize( ref T array, int newSize ) 这是一种泛型表示形式。其中T表示数组类型,array为要调整大小的一维数组,该数组从零开始;如果为null则新建具有指定大小的数组。如果newSize大于旧数组的Length,则分配一个新数组,并将所有元素从旧数组复制到新数组。如果newSize小于旧数组的Length,则分配一个新数组,并将元
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 常用 数据类型 用法
链接地址:https://www.31doc.com/p-2254622.html