C# 字符串处理

  • 之前做的一些关于字符串处理的笔记

str.Length 访问一个字符串的长度

string res = str.ToLower(); 把字符串全部转换成小写,并返回,对原字符串没有影响

string res = str.ToUpper(); 把字符串全部转换成大写,并返回,对原字符串没有影响

string res = str.Trim() 去掉字符串首尾空格 防止不必要的空格导致错误

例如: "     af s    " 转换为 "af s"

string res = str.TrimStart() 去掉字符串首空格

string res = str.TrimEnd() 去掉字符串尾空格

string[] strArray = str.Split(".") 把原字符按照指定字符进行拆分,得到一个拆分后的字符串数组

if (IsNullOrEmpty(字符串)) 它相当于 if(s==""||s==null)

string.IsNullOrWhiteSpace这个是判断所有空白字符

String.SubString(int index,int length) 取出一段字符,index:开始位置,从0开始 length:你要取的子字符串的长度

例:
string abc = "123456789";
string def = abc.Substring(1, 5);
Console.WriteLine("def:" + def);
打印 "def: 23456"

Replace替 换字符和字符串

例:
 string s1 = "HelloC#";            
 string s2 = s1.Replace('C', 'c');	//用字符c替换C            
 string s1 = "ab    c";            
 string s2 = s1.Replace(" ", "");	// 将空格去掉            

Insert 插入字符串

例:
string s1 = "tree";
string s2 = s1.Insert(1, "h");  //在字符串s1 下标为1的位置 插入一个字符串

Remove 删除字符串

例:
string s1 = "0123456789";
s1 = s1.Remove(4);          //删除从下标为4的位置开始到末尾的所有字符

string s1 = "0123456789";
s1 = s1.Remove(4, 4);       //删除从下标为4的位置开始的4个字符
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值