1.数组
- 声明
baseType[] name; - 初始化
int[] GodSLife = { 20, 20, 20, 20, 20 };
int[] GodSLife; GodSLife = new int[5];
int[] GodSLife; GodSLife = new int[5] { 20, 20, 20, 20, 20 };
- 得到数组长度
name.Length - 遍历数组的方式
foreach会依次取到数组中的值,赋值给temp,然后执行循环体
格式:
foreach(int temp in name)
{
Console.WriteLine(temp);
}
foreach中的in表示我们要遍历哪个数组,temp表示我们要访问的各个数组元素,其类型根据数组类型而定(比如此处是int),但foreach不知道数组元素的索引
2.字符串的处理
字符串可以当作字符char类型的数组
- 可以通过name.Length访问字符串的长度
- 可以通过name[index]访问字符串指定索引位置处的字符
- name.ToLower()把字符串转换成小写并返回
- name.ToUpper()把字符串转换成大写并返回
- name.Trim()去掉字符串前面和后面的空格
- name.TrimStart()去掉字符串前面的空格
- name.TrimEnd()去掉字符串后面的空格
以上函数均对原字符串无影响,需用另一个string类型变量接收
- name.Split()把原字符串按照指定的字符进行拆分,得到一个拆分后的字符串数组,指定的字符放在单引号里面
Split访问的是字符串数组,要用字符串数组接收(不能用string类型变量)
例如:
string str = "my-mind-will-be-concentrated";
string[] str2 = str.Split('-');
Console.WriteLine(str + ".");
foreach(string temp in str2)
{
Console.WriteLine(temp);
}
输出为