2013-9-4c#知识总结
流程控制和字符串处理
一.大小写转换函数
string str="Abcdef";
string newstr=str.ToUpper(); //把str指向的字符串全部转换成大写
Console.WriteLine(newstr);
string smallstr=str.ToLower(); //把str指向的字符串全部转换成小写
Console.WriteLine(smallstr);
Console.ReadKey();
二.去除空格
Console.WriteLine("前面"+newster+"后面");
string newstr1=newstr.Trim(); //Trim去除前面和后面的空格
Console.WriteLine("前面"+newster1+"后面");
三.变量和值之间的关系
情况1.string str="Abcdef";
string newstr=str.ToUpper(); //把str指向的字符串全部转换成大写
Console.WriteLine(newstr);
情况2.string str="Abcdef";
str=str.ToUpper(); //把str指向的字符串全部转换成大写
Console.WriteLine(str);
四.Trim方法
Console.WriteLine("请输入");
string input=Console.ReadLine();
char[] strs=new char[2]{'s','h'}; //数组形式的,删除多个字符
char[] strs={'s','h'}; //这样也可以,同上
input=input.Trim('s','h'); //不用数组形式,也可以
input=input.Trim(strs);
Console.WriteLine(input);
Console.ReadLine();
五.读取文件并显示
string content=File.ReadALLText("C:\\学生.txt",Encoding.Default) // 读取文件
(//从文本文件中读取所有行,如果有n行,就会创建一个大小为n的数组,然后逐行放到数组中,此时数组中的每一个元素都存储文件中的每一行)
string content=File.ReadALLLines("C:\\学生.txt",Encoding.Default) //显示
for(int i=0;i<content.Length;i++)
{
string[] nameage=content[i].Split('|'); //将每一个元素(name|age),按照"|"进行分割,放到数组nameage中,此时namgeage[0]就存储姓名,namgeage[1]就存储年龄
Console.WriteLine("学生的姓名是"+nameage[0]+"年龄为"+namesge[1]);
}
Console.Readkey();
五.条件语句和循环
条件语句1. if语句,switch语句
//if语句和else if的区别:else if执行效率要高,如果前面的条件满足了,就不会判断后面的else if了。if语句则是每个If语句块都要执行一遍,哪怕前面的条件早满足了。
Console.WriteLine("输入目标距离,单位KM");
double juli =Convert.ToDouble(Console.ReadLine());
if (juli <= 3)
{
Console.WriteLine("建议步行,锻炼身体,健美身材");
}
else if (juli <= 10 && juli > 3)
{
Console.WriteLine("建议骑单车");
}
else if (juli > 10)
{
Console.WriteLine("建议乘坐交通工具");
}
//所有switch语句能完成的功能都可以使用If,反之则不然
Console.WriteLine("输入明星姓名,我会说出他的职业");
string name = Console.ReadLine();
switch (name)
{
case "文章":
Console.WriteLine("大明星啊");
break;//执行完当前语句则退出switch语句
case "林志玲":
Console.WriteLine("大美女啊");
break;
case "郭德纲":
Console.WriteLine("非著名相声演员");
break;
default:
Console.WriteLine("爱谁谁,我不认识");
break;
}
Console.ReadKey();
循环2. for循环,foreach,while,do .....while
//c#代码是从上到下执行的
//do....while循环:无论while()中的条件是否为真,do{}中的代码都会预先执行一遍
/*int i = 1;
do
{
Console.WriteLine(i);
i++;
} while (i>=10);*/
int i = 1;
while (i<=10)
{
Console.WriteLine(i);
i++;
}
Console.ReadKey();