- 2
- 0
- 约6千字
- 约 7页
- 2021-10-13 发布于福建
- 举报
C#经常用到的几个截取字符串
C#几个经常用到的字符串的截取
string str=quot;123abc456quot;;
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 判断字符串中是否有 quot;abcquot; 有则去掉之
using System.Text.RegularExpressions;
string str = quot;123abc456quot;;
string a=quot;abcquot;;
Regex r = new Regex(a);
Match m = r.Match(str);
if (m.Success)
{
// 下面两个取一种即可。
str=str.Replace(a,quot;quot;);
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 如果字符串中有 quot;abcquot; 则替换成 quot;ABCquot;
str=str.Replace(quot;abcquot;,quot;ABCquot;);
************************************************
string str=quot;adcdefquot;; int indexStart =
str.IndexOf(quot;dquot;);
int endIndex =str.IndexOf(quot;equot;);
string toStr = str.SubString(indexStart,endIndex-indexStart);
c#截取字符串最后一个字符的问题 !
str1.Substring(str1.LastIndexOf(quot;,quot;)+1);
C# 截取字符串最后一个字符
k = k.Substring(k.Length-1, 1);
lt;table width=quot;1000quot; height=quot;250quot;
border=quot;0quot;gt;
lt;trgt;
lt;td align=quot;centerquot; colspan=quot;5quot;gt; lt;font
size=quot;+3quot; gt;lt;stronggt; 请 假 条
lt;/stronggt;lt;/fontgt;lt;hr color=quot;#000000quot;
width=quot;500quot;gt;lt;hr color=quot;#000000quot;
width=quot;500quot;gt;lt;/tdgt;lt;/trgt;
lt;trgt;lt;td align=quot;rightquot;gt; 单 位 :
lt;/tdgt;lt;tdgt;lt;input
您可能关注的文档
最近下载
- 河道整治工程施工质量创优计划的实施效果评估与改进策略.docx VIP
- 水利工程质量创优方案(3篇).docx VIP
- 110kv变电站的设计毕业设计(论文).docx VIP
- 华中师范大学《C++程序设计》2020-2021学年第一学期期末试卷.pdf VIP
- 电力系统仿真:电力系统暂态仿真all.docx VIP
- 电力系统暂态稳定性仿真研究毕业设计(论文).docx VIP
- 3 浮力——液面升降问题(原卷版).docx VIP
- 核心素养视域下的初中数学单元作业设计研究.docx VIP
- 纪念红军长征90周年演讲稿(2025年最新版).pptx VIP
- 2002-2012 十年北京卷高考语文试卷深度解析.pdf VIP
原创力文档

文档评论(0)