博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#截取字符串
阅读量:4315 次
发布时间:2019-06-06

本文共 1153 字,大约阅读时间需要 3 分钟。

几个经常用到的字符串的截取

string str="123abc456";int i=3;

1 取字符串的前i个字符
   

str=str.Substring(0,i); // or  str=str.Remove(i,str.Length-i);

2 去掉字符串的前i个字符:
 

str=str.Remove(0,i);  // or str=str.Substring(i);

3 从右边开始取i个字符:

str=str.Substring(str.Length-i); // or str=str.Remove(0,str.Length-i);

4 从右边开始去掉i个字符:

str=str.Substring(0,str.Length-i); // or str=str.Remove(str.Length-i,i);

5 判断字符串中是否有"abc" 有则去掉之

  

using System.Text.RegularExpressions;   string str = "123abc456";   string a="abc";   Regex r = new  Regex(a);    Match m = r.Match(str);    if (m.Success)   {    //绿色部分与紫色部分取一种即可。      str=str.Replace(a,"");      Response.Write(str);         string str1,str2;      str1=str.Substring(0,m.Index);      str2=str.Substring(m.Index+a.Length,str.Length-a.Length-m.Index);      Response.Write(str1+str2);    }

6 如果字符串中有"abc"则替换成"ABC"

str=str.Replace("abc","ABC");
//************************************************ string str="adcdef"; int indexStart = str.IndexOf("d");int endIndex =str.IndexOf("e");string toStr = str.SubString(indexStart,endIndex-indexStart);c#截取字符串最后一个字符的问题!str1.Substring(str1.LastIndexOf(",")+1)

 

转载:

    

转载于:https://www.cnblogs.com/wangfuyou/p/5102079.html

你可能感兴趣的文章
控件中添加的成员变量value和control的区别
查看>>
Spring Boot Docker 实战
查看>>
Div Vertical Menu ver3
查看>>
Git简明操作
查看>>
InnoDB为什么要使用auto_Increment
查看>>
HDU 1087 Super Jumping! Jumping! Jumping!
查看>>
0007_初始模块和字节码
查看>>
[效率提升]如何管理好你的电脑文件
查看>>
C++实验二
查看>>
使用case语句给字体改变颜色
查看>>
JAVA基础-多线程
查看>>
面试题5:字符串替换空格
查看>>
JSP九大内置对象及四个作用域
查看>>
ConnectionString 属性尚未初始化
查看>>
MySQL基本命令和常用数据库对象
查看>>
poj 1222 EXTENDED LIGHTS OUT(位运算+枚举)
查看>>
进程和线程概念及原理
查看>>
Lucene、ES好文章
查看>>
android 生命周期
查看>>
jquery--this
查看>>