"hello world"字符串 ' h'字符 空格也算字符
bool取值false和true,是0和1的区别;false可以代表0,但true有很多种,并非只有1。
c#中 布尔类型非0为真,只有0为假
string char
string str = "string";
字符串长度
int a = str.Length;
Console.WriteLine ("{0}",a); //a = 6
如何判断两个字符串恒等
string str1 = "ni hao";
if (str1 == str){
}
bool b = str1.Equals (str);
if (b) {
}
字符串的拼接
string str2 = str + str1; //"1"+" "+"2" = "1 2";
string ZhangHao = "ni hao";
Console.WriteLine ("输入帐号");
string ZhangHao1 = Console.ReadLine ( );
if (ZhangHao1 == ZhangHao) {
Console.WriteLine ("帐号正确");
} else {
Console.WriteLine ("错误");
}
//登录
string name = "admin",password = "123456";
Console.WriteLine ("输入帐号");
string user_name = Console.ReadLine ( );
Console.WriteLine ("输入密码");
string user_password = Console.ReadLine ( );
// if (user_name == name && user_password == password) {
// Console.WriteLine ("真聪明");
// } else {
// Console.WriteLine ("大傻逼");
// }
if (user_name == name) {
if (user_password == password) {
Console.WriteLine ("小宝贝真聪明");
} else {
Console.WriteLine ("密码错了大傻逼");
}
} else {
Console.WriteLine ("用户名错了臭傻逼");
}
//判断某头字符串是否包含某个字符串
string str = "hellow";
str.Contains ("el");
bool b = str.Contains ("el");
Console.WriteLine ("{0}",b); // true
//是否以某一个字符串结尾
str.EndsWith ("o");
//是否以某一个字符串开始
str.StartsWith ("h");
//右对齐
string newstar = str.PadLeft(15);
Console.WriteLine (newstar);
string str1 = "China";
string str2 = "English";
Console.WriteLine (str1.PadLeft(5)); //左缩进5个字符
Console.WriteLine (str2.PadLeft(5));
//替换
string str = "hellow";
;string str3 = str.Replace ("ll","abcd");
Console.WriteLine (str3); //heabcdow
//转换为大写
string upper_Str = str.ToUpper();
Console.WriteLine (upper_Str); //HELLOW
//a转A ASCLL码控制
char n = (char)((int)'a' - 32);
Console.WriteLine ("{0}",n); //A
string str = " hellow world ";
//从当前的字符串中去移除头部和尾部的空白字符
string str1 = str.Trim();
Console.WriteLine ("{0}",str1);
//截取字符串
string str2 = str1.Substring(4,4); //从第4个字符开始(不包含第4个) 截取长度为4
Console.WriteLine("{0}",str2);
Console.WriteLine (str.Remove (1,3)); //删除下标为1 后面三个数(包括1)
Console.WriteLine(str.Substring(1,3)); //截取下标为1 后面三个数(包括1);
//分割字符串
string str = "Stritritring ";
char[] chs = { 'i' }; //以i作为分割标识
string[] strs = str.Split(chs);
foreach (string s in strs) {
Console.WriteLine (s); //Str tr tr ng
}