字符串类型
一、string
//打出s.时就会出现一堆的方框,要找不带箭头的(不带箭头的是我们现在可以用的到的),不要找带箭头的(带箭头的是扩展,现在还用不到)
//不带箭头的都是对s的操作(动作和行为),就好比s是人有很多的动作和行为。
//只要是动作和行为;不带箭头;的要加小括号()
1、Trim() - 去头尾的空格,中间的空格不能去。
例子:
static void Main(string[] args) //字符串类型
{
string s = " Hello World "; //左边右边都有空格 " Hello World "
Console.WriteLine(s.Trim() + "aaa"); //Trim() -去左边右边的空格的 。 去头尾的空格,中间的空格不能去。 }
显示的结果
2、ToUpper() - 把字符串的字母全变成大写。
例子:
static void Main(string[] args)
{
string s = "Hello World";
Console .WriteLine (s.ToUpper()); //ToUpper() - 把字符串的字母全变成大写。
显示的结果:
ToLower() - 把字符串的字母全变成小写。
例子:
static void Main(string[] args)
{
string s = "Hello World"; Console.WriteLine(s.ToLower()); //ToLower() - 把字符串的字母全变成小写。
}
显示的结果:
3、StartsWith("字符串") - 是否以“子串”开头,是-true
例子:
static void Main(string[] args)
{
string s = "Hello World";
Console.WriteLine(s.StartsWith("He")); //StartsWith("字符串") - 是否以“子串”开头,是-true }
显示的结果:
EndsWith("子串") - 是否以“子串”结尾,是-true
static void Main(string[] args)
{
string s = "Hello World";
Console.WriteLine(s.EndsWith("ld"));//EndsWith("子串") - 是否以“子串”结尾,是-true }
显示的结果:
Contains("子串") - 是否包含“子串”,是-true
static void Main(string[] args)
{ string s = "Hollo World";
Console.WriteLine(s.Contains("o")); //Contains("子串") - 是否包含“子串”,是-true
//显示的结果是true
}
显示的结果:
Substring(起始位置,长度) - 从大字符串中,截取小的子串出来。
static void Main(string[] args)
{ string s = "Hollo World";
Console.WriteLine(s.Substring(, )); //Substring(起始位置,长度) - 从大字符串中,截取小的子串出来。 }
显示的结果:
4、IndexOf("子串") - 从大字符串中,找到子串第一次出现的位置。返回整数。如果大串中找不到小串,返回-1
static void Main(string[] args)
{ string s = "Hello World";
int b = s.IndexOf("o"); //IndexOf("子串") - 从大字符串中,找到子串第一次出现的位置。返回整数。如果大串中找不到小串,返回-1
Console.WriteLine(b);
}
显示的结果:
LastIndexOf("子串") - 从大字符串中,找到子串最后一次出现的位置。返回整数。如果大串中找不到小串,返回-1、
static void Main(string[] args)
{ string s = "Hello World";//LastIndexOf("子串") - 从大字符串中,找到子串最后一次出现的位置。返回整数。如果大串中找不到小串,返回-1
int b = s.IndexOf("d");
Console.WriteLine(b);
//显示的结果是:10
}
显示的结果:
ReplaceWith("被替换的子串","新的子串"):把大串中指定的小串,换成另一小串,返回替换后的大串。
static void Main(string[] args)
{ string s = "Hello World";
//ReplaceWith("被替换的子串","新的子串"):把大串中指定的小串,换成另一小串,返回替换后的大串。
Console.WriteLine(s.Replace(" ", "_")); //把空格,替换成下划线。
}
显示的结果:
练习1:
您输入的QQ邮箱是否正确?
static void Main(string[] args)
{ Console.WriteLine("请输入您的QQ邮箱:"); //显示打印的 string youxiang = Console.ReadLine(); //我们要输的 bool shi = true; if (youxiang.EndsWith("@qq.com") == shi) //判断是不是以@qq.com 结尾的 {
Console.WriteLine("您输入的QQ邮箱正确");
} else {
Console.WriteLine("你输入的QQ邮箱不正确");
} }
练习2、
从身份证中找出你的生日
static void Main111111(string[] args) //身份证号,从中找出年、月、日
{
Console.WriteLine("请您输入身份证号码");
string sfz = Console.ReadLine(); string nianfen = sfz.Substring(, ); //(6,4)表示第几位的数,取几个。
string yue = sfz.Substring(, );
string ri = sfz.Substring(, ); Console.WriteLine("您的生日是:" + nianfen + "年" + yue + "月" + ri + "日");
Console.ReadLine(); }