趁着假期巩固下刚学的C#,保存些小例子来记录下这个过程吧!
在vs下(我自己用的是08)——新建项目——c#控制台应用程序之后就copy过去按F5看结果了~希望能对初学c#的伙计们有点帮助~
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
/*声明字符串*/
string str = "Japril.W";
string str2 = "C#";
string str3 = "Programmer";
string str4 = "Beijing,London,Washington,Paris,Toronto";
/*数组 基本操作*/
string[] groups = { "Asp.net", "C#", "mvc", "wcf", "wpf", "linq" };//数组的初始化
int count = groups.Length; //获取数组长度
Console.WriteLine("----------------数组の长度-------------------");
Console.WriteLine(count.ToString()); //将获取的数组长度输出
Console.WriteLine("----------------原数组元素值-------------------");
for (int i = 0; i < count; i++) //数组的遍历
{
Console.WriteLine(groups[i]);//逐一输出数组中的每个元素
};
/*字符串 基本操作*/
Console.WriteLine("Hello!I'm {0},let's learn {1} 2gether!", str, str2);//格式化多个字符串
if (str == str2) //字符串比较法(1)——用“==”比较
{
Console.WriteLine("字符串长度相等(^0^)Y");
}
else
{
Console.WriteLine("字符串长度不等(ToT)");
}
if (str.CompareTo(str2) > 0) //字符串比较法(2)——用“Compare”方法比较
{
Console.WriteLine("字符串长度不等(ToT)");
}
else
{
Console.WriteLine("字符串长度相等(^0^)Y");
}
Console.WriteLine(str + " is a " + str2 + " " + str3); //字符串的连接
Console.WriteLine(str4.ToUpper()); //字符串转换_成大写
Console.WriteLine(str4.ToLower()); //字符串转换_成小写
string[] p = str4.Split(','); //字符串拆分——用“Split”方法分割字符串并存入数组p
for (int i = 0; i < p.Length; i++) //遍历p
{
Console.WriteLine(p[i]);
}
if (String.IsNullOrEmpty(str4)) //字符串是否为空——用String类的静态方法
{
Console.WriteLine("字符串为空!");
}
else
{
Console.WriteLine("字符串不为空!");
}
str4 = str4.Replace("Washington", "WinDOT"); //字符串的替换——用“Replace”方法
Console.WriteLine(str4);
Console.ReadKey(); //等待user输入
}
}
}
本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/japril1988/archive/2010/01/24/5250602.aspx