c# 字符串_数组与变量类型
字符串内容查找方法
//索引从0开始
string email = "xiaoqiang@qq.com";
int position = email.IndexOf("@");
Console.WriteLine("@所在位置索引是: {0}",position);
获取字符串的长度
string email = "xiaoxiong@qq.com";
int pwdLength = email.Length;
Console.WriteLine("字符串长度是:" + pwdLength);
判断字符串是否相等
// 使用"==" 或 "Equals()" 方法
string name1 = "xiaowang";
string name2 = "xiaowang";
string name3 = "wang";
Console.WriteLine(name1 == name2);
Console.WriteLine(name1.Equals(name2));
Console.WriteLine(name2 == name3);
Console.WriteLine(name2.Equals(name3));
字符串的截取
string email = "xiaoqiang@qq.com";
string userName = email.Substring(0, email.IndexOf("@"));
Console.WriteLine("邮箱用户名:"+userName);
格式化字符串的方法
string name = "小刘";
Console.WriteLine("我的名字是" + name + " 我的年龄是 " + 24);
Console.WriteLine("我的名字是{0},我的年龄是{1}",name,24);
Console.WriteLine("**********************************");
//Format()方法的应用
string name1 = "小文";
int age = 24;
string userAge = "我的名字是{0},我的年龄是{1}";
userAge = string.Format(userAge,name1,age);
Console.WriteLine(userAge);
空字符串的使用方法
string name = ""; //分配一个存储空间,并存入""
string name1 = string.Empty;//和""一样,长度为0,更易读
string name2 = null; //仅仅是变量的声明,对象不存在
string name3; //仅仅是变量的声明,对象不存在
其他常用字符串处理方法
//去掉前后多余空格
string name = " xiaowang ";
string name1 = name.Trim();
Console.WriteLine(name1);
//转换成大写
string name2 = name.ToUpper();
Console.WriteLine(name2);
//转换成小写
string name3 = name.ToLower();
Console.WriteLine(name3.Trim());
//找到最后一个匹配项所在的索引(位置)
string url = "http:www.baidu.com/name=dsasd&.pwd=aaa999&.aspx";
int position = url.LastIndexOf(".");
Console.WriteLine(position);
字符串的高效处理
//关于字符串的拼接
string stuText = "我正在学习";
stuText += ".NET平台";
stuText += "与C#开发语言";
Console.WriteLine(stuText); //内存浪费;
//关于StringBuilder类的使用
//StringBuilder类定义可变字符串,实现字符串的追加的相关方法
//Append 在结尾追加
//Insert 在指定位置插入指定字符串
//Remove 移除指定字符串
//实例化一个StringBuilder对象
StringBuilder myBuilder = new StringBuilder();
myBuilder.Append("我正在学习");
myBuilder.Append(".NET平台");
myBuilder.Append("与c#开发语言");
//转成string类型
Console.WriteLine(myBuilder.ToString());
数组的组成
//使用数组的4个基本步骤
//【1】声明数组
int[] score;
//【2】分配空间
score = new int[5];
//【3】赋值
score[0] = 67;
//【4】处理数据
score[0] = score[0] + 5;
Console.WriteLine(score[0]);
数组声明同时初始化的三种情况
int[] netScore1 = new int[3] { 67, 68, 69 };
int[] netScore2 = new int[] { 67, 68, 69 };
int[] newScore3 = { 67, 68, 69 };
数组应用
//声明数组并赋值
int[] netScore = { 67, 89, 78, 69, 95 };
int sumScore = 0;
//使用for循环读取数组元素
for(int i=0;i<netScore.Length;i++)
{
sumScore += netScore[i];
}
int avgScore = sumScore / netScore.Length;
Console.WriteLine("学员总分是 "+sumScore);
Console.WriteLine("学员平均成绩"+avgScore);
遍历数组的新方法
//foreach相对与for循环更简洁,在遍历数组可以考虑使用。
double[] netScore = { 77.6, 88.9, 99.6, 87.4, 60.7 };
double sumScore = 0;
foreach(double score in netScore)
{
sumScore += score;
}
Console.WriteLine(sumScore);
double avgScore = sumScore / netScore.Length;
Console.WriteLine(avgScore);
字符串的处理补充方法
//使用空格分割字符串
string name1 = "x i a o w a n g";
string[] letteArray = name1.Split();
Console.WriteLine(letteArray.Length);
//使用逗号分割字符串
string name2 = "小王,小刘,小张";
string[] nameList = name2.Split(",");
string name3 = string.Join("+", nameList);
Console.WriteLine(name3);
Console.WriteLine("*****************");
值类型的变量
int wangScore = 90;
int zhangScore = wangScore;
Console.WriteLine("小王成绩{0} 小张成绩{1}",wangScore,zhangScore);
//修改小张的成绩
//变量zhangScore重新开辟新的内存空间,并将90复制到新空间中
//基本数据类型在传递变量值时,传递是变量的"副本",而不是变量本身,
//变量修改后相互没有影响
zhangScore += 5;
Console.WriteLine("小王成绩{0} 小张成绩{1}", wangScore, zhangScore);
引用类型的变量
int[] score = { 90, 90 };
Console.WriteLine("审阅前二人成绩:小王{0} 小张:{1}", score[0], score[1]);
//修改小张的成绩
int[] editedScore = score;
editedScore[1] += 5;
Console.WriteLine("审阅前二人成绩:小王{0} 小张:
{1}",editedScore[0],editedScore[1]);
Console.WriteLine("------------------------------------------");
Console.WriteLine("审阅前二人成绩:小王{0} 小张:{1}", score[0], score[1]);
//数组是引用变量!!!
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。