字符串提供的各种方法

length:获得当前字符串中字符的个数

//练习一:随机输入你心中想到的一个名字,然后输出它的字符串长度 
 Length:可以得字符串的长度
Console.WriteLine("请输入你心中想到的名字");
string name=Console.ReadLine();
Console.WriteLine(name.Length);
Console.ReadKey();

Toupper:将字符串转换成大写形式

ToLower:将字符串转换成小写形式

Console.WriteLine("请输入你喜欢的课程");
string lessone=Console.ReadLine();
//因为输入的课程可能会大小写,所有就全转换成大写或者小写
lessone=lessone.Toupper();
Console.WriteLine("请输入他喜欢的课程");
string lesstwo=Console.ReadLine();
lessstwo=lesstwo.ToLower();
if(lessone=lesstwo)
{
Console.WriteLine("你们两个喜欢的课程相同,都是{0}",lessone);
}
else
{
Console.WriteLine("你们两个喜欢的课程不同,一个喜欢{0},一个喜欢{1}",lessone,lesstwo);
}
Console.ReadKey();

spilt:分割字符串,返回字符串类型的数组

string str="a b ddg _+=,,,fdasd"
char[] chs={' ','_','+','+',','}
string[]str=str.split(chs,stringsplitoptions.RemoveEemptyEntries);
Console.WriteLine(str);
Console.ReadKey();
//练习:从日期字符串("2008-08-08")中分析出年、月、日;2008年08月08日。
//让用户输入一个日期格式如:2008-01-02,你输出你输入的日期为2008年1月2日。
string date="2008-01-02";
char[]chs={'-'}
string[]Date=date.Split(new char[]chs{'-'},StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("{0}年{1}月{2}日",Date[0],Date[1],Date[2]);
Console.ReadKey();

contains:判断字符串是否含有子串value Replace:(old value,new value)

string str="国际关键人物老赵";
if(str.contains("老赵"))
{
str=str.Replace("老赵","**")
}
Console.WriteLine(str);
Console.ReadKey();

substring:截取字符串

string str="今天天气好晴朗,处处好风光";
str=str.substring(0,4);
Console.WriteLine(str);
Console.ReadKey();

startswith(" "):是否以某字符串开头

Endswith(" "):是否以某字符串结尾

string str="今天天气好晴朗,处处好风光"
if(str.startswith("今天")
{
Console.WriteLine("是")
}
else
{
Console.WriteLine("不是")
}
Console.ReadKey();

Tirm:去字符串前后的空格

Trimstart:去字符串前的空格

Trimend:去字符串后的空格

string str="   dhajd  "
str=str.Trim;
Console.WriteLine(str);
str=str.Trimstart;
Console.WriteLine(str);
str=str.Trimend;
Console.ReadKey();

IsNullorEmpty:判断字符串是否有空格

string str="dad  "
if(str.NullorEmpty)
{
Console.WriteLine("有空格")
}
else
{
Console.WriteLine("没有空格")
}
Console.ReadKey();

Indexof(" "):判断某字符串在字符串中第一次出现的位置,没有则返回-1

LastIndexof(" "):判断莫字符串在字符串最后一次出现的位置,没有则返回-1

string str="今天天气好晴朗,处处好风光";
int n=str.Indexof("天气");
Console.WriteLine(t);
int l=str.Indexof("处处");
Console.WriteLine(l);
Console.ReadKey();

join:将字符串或者字符串数组按照指定的字符串连接,返回一个字符串

string[]names={"张三","王五","赵六","李四"};
string Names=string.join("|",names);
Console.WriteLine(Names);
Console.ReadKey();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值