C程序设计-数据类型和数组.ppt
文本预览下载声明
用IndexOf()方法,查找子串 字符串类型(string) string s9 = Battle of Hastings, 1066; // outputs 10 System.Console.WriteLine(s9.IndexOf(Hastings)); // outputs -1 System.Console.WriteLine(s9.IndexOf(1967)); 用Split()方法分解串中单词 字符串类型(string) char[] delimit = new char[] { }; string s10 = The cat sat on the mat.; string[] words = s10.Split(delimit); foreach (string substr in words) { System.Console.WriteLine(substr); } 输出结果 The cat sat on the mat. 字符串类型(string) 在 .NET Framework中string是类String的别名 方法:下表介绍一些最常用的String 类的方法 常用方法 说明 IndexOf 查长子串 CopyTo 将源串中指定部分串拷贝到目标串中 SubString 在串中取子串 Split 分解串成单词 Replace 用新串替换串中指定的串 ToUpper 将串中所有小写字母转换成大写 ToLower 将串中所有大写字母转换成小写 Trim 删除串中头、尾指定的字符 1、将任意一字符串反序输出,并且每个字符都大写。 2、求将某一字符串str中的所有子序列smod都删除后所得的新串newStr? 例如: str = “H234llo,Wo234rld234!”; smod = “234”; 求新串 newStr ? 字符串类型(string) 思考题? 数组(Array) 数组类型属于复合数据类型,它是由一组顺序存储的同一类型的元素组成的数据集合。 特点: 元素类型相同; 元素有顺序; 所有元素共用一个名称。 数组的声明 格式: type[ ] arrayName; 举例: int[ ] num; double[ ] array_double; string[ ] str; Point[ ] P; 数组的创建 在C#中,声明数组时不能指定它的长度,而是利用new 运算符来为数组型变量分配内存空间,我们将其称之为创建数组。 num = new int[3]; array_double = new double[1000]; string[ ] str = new string[5]; 数组创建后,系统自动为数组元素赋初值。 数组元素默认值 Value Type Default Value 整型 0 实型 0.0F 或 0.0D 或 0.0M 字符型 ‘\0’ 布尔型 false 引用型 null 数组创建后的元素默认初值。 数组(Array) 基本数据类型一维数组内存分配 栈内存 堆内存 num 0 0 0 0088:4400 0088:4400 new int[3]产生的对象 int[] num; num=new int[3]; 数组(Array) 基本数据类型一维数组内存分配 栈内存 堆内存 num 0 0 0 null 0088:4400 new int[3]产生的对象 int[] num; num=new int[3]; num=null; 数组(Array) 基本数据类型一维数组初始化 class TestArrays { public static void Main(){ int[] array1; array1=new int[3]; //默认值为0 int[] array2=new int[3]; int[] array3={5,9,10}; int[] array4=new int[3]{5,9,10}; int[] array5=array4; //两数组指向同一内存区 } } 数组(Array) 对象数组的内存分配 class Student{ private string name; private int age; public Student(st
显示全部