c#小知识点

整理一些比较实用的代码;

1.Thread.Sleep(100);  //休眠 单位 毫秒
2.Console.Clear();     // 清屏

3.foreach (int z in num)   //从索引0位置遍历数组至结束为止
{
Console.WriteLine(z);
}

等价于:

for (int i = 0; i < num.Length; i++)
{
Console.WriteLine(num[i]);

}

4.描述按下的控制台键

ConsoleKeyInfo key = Console.ReadKey();
string s = key.KeyChar.ToString();

Key :获取当前 ConsoleKeyInfo 对象表示的控制台键

KeyChar:获取当前 ConsoleKeyInfo 对象表示的 Unicode 字符。

 5.字符串操作

l.length 字符串长度,字符串个数
Console.WriteLine(s.Length );
Console.WriteLine(s.TrimStart()); //去掉前面的空格
s.TrimEnd();//去掉后边的空格
Console.WriteLine(s.ToLower ());//转换成小写
Console.WriteLine(s.ToUpper());//转换成大写
bool isbaohan = s.Contains("if");//判断字符串中是否包含某字符段
Console.WriteLine(isbaohan);
//字符串有长度,有索引 索引从0开始到length-1结束

int index = s.IndexOf("if",4);
Console.WriteLine(index);
int lastid = s.LastIndexOf("as");
Console.WriteLine(lastid );


string jqs = s.Substring(4,3);//(截取的索引位置,截取个数)
Console.WriteLine(jqs);

 

posted @ 2016-04-01 09:08  鱼在我这里  阅读(94)  评论(0编辑  收藏  举报