cnblogs/dcrenl/c#.net常见字符串处理方法.html

7 lines
3.8 KiB
HTML
Raw Normal View History

2024-09-24 12:43:01 +08:00
<div>1.字符串比较<br />字符串.ComparTo(目标字符串)<br />"a".ComparTo("b");<br />2.查找子串<br />字符串.IndexOf(子串,查找其实位置) ;<br />字符串.LastIndexOf(子串) ;最后一次出现的位置<br />str.IndexOf("ab",0);<br />3.插入子串<br />字符串.Insert(插入位置,插入子串) ;<br />s.Insert(2,"ab");<br />4.移出子串<br />字符串.Remove其实位置移出数;<br />s.Remove(3,2);<br />5.替换子串<br />字符串.Replace源子串替换为;<br />s.Replace("-"," ");将-替换为空格<br />6.截取子串<br />字符串.Substring截取其实位置截取个数;<br />"abcdef".Substring(2,3);结果为cde<br />7.去空格<br />myString = myString.Trim(); //同时删除字符串前后的空格<br />char[] trimChars = {' ','e','s'}; //准备删除的字符<br />myString = myString.Trim(trimChars); //删除所有指定字符<br />myString = myString.TrimEnd(); //删除字符串后的空格<br />myString = myString.TrimStart(); //删除字符串前的空格<br />8.转换大小写<br />str.ToLower()转化成小写字母<br />str.ToUpper()转化成大写字母<br />Char.IsUpper(str,3)//判断一个字符串中的第n个字符是否是大写<br />9.拆分字符串<br />string[] Strs = myString.Split(' ',3); //按照空格进行拆分,并且返回前三个字符串,结果在字符串数组里<br />10.使字串达到指定长度<br />PadLeft()、PadRight() ,如:<br />string str1="00";<br />str1=str1.PadLeft(3,'1'); //无第二参数为加空格,结果为&ldquo;100&rdquo;<br />11.得到字符串长度<br />len=str.Length;<br />12.字符串遍历<br />string myString = "This is a test!";<br />foreach (char myChar in myString)<br />{<br />Console.Write("{0}",myChar);<br />}&nbsp;</div>
<div>----------------------------------------------------------------------------------------</div>
<div>
<h1>.net中几个经常用到的字符串的截取</h1>
</div>
<div>string str="123abc456";<br />int i=3;<br />1 取字符串的前i个字符<br />&nbsp;&nbsp;&nbsp;<span style="font-family: Verdana;">str=str.Substring(0,i); // or&nbsp;&nbsp;str=str.Remove(i,str.Length-i);&nbsp;<br />2 去掉字符串的前i个字符<br />&nbsp;&nbsp;&nbsp;str=str.Remove(0,i);&nbsp;&nbsp;// or str=str.Substring(i);&nbsp;<br />3 从右边开始取i个字符<br />&nbsp;&nbsp;</span><span style="font-family: Verdana;">str=str.Substring(str.Length-i); // or str=str.Remove(0,str.Length-i);<br />4 从右边开始去掉i个字符<br />&nbsp;&nbsp;&nbsp;str=str.Substring(0,str.Length-i); // or str=str.Remove(str.Length-i,i);<br />5 判断字符串中是否有"abc" 有则去掉之<br />&nbsp;&nbsp;&nbsp;using System.Text.RegularExpressions;<br /></span><span style="font-family: Verdana;">&nbsp;&nbsp;&nbsp;string str = "123abc456";<br />&nbsp;&nbsp;&nbsp;string a="abc";<br />&nbsp;&nbsp;&nbsp;Regex r = new&nbsp;&nbsp;Regex(a);&nbsp;<br />&nbsp;&nbsp;&nbsp;Match m = r.Match(str);&nbsp;<br />&nbsp;&nbsp;&nbsp;if (m.Success)<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: Verdana;">//绿色部分与紫色部分取一种即可。<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str=str.Replace(a,"");<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Write(str);&nbsp;&nbsp;&nbsp;<br /></span><span style="font-family: Verdana;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string str1,str2;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str1=str.Substring(0,m.Index);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str2=str.Substring(m.Index+a.Length,str.Length-a.Length-m.Index);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Write(str1+str2);&nbsp;<br />&nbsp;&nbsp;&nbsp;}</span><br />6 如果字符串中有"abc"则替换成"ABC"<br />&nbsp;&nbsp;&nbsp;<span style="font-family: Verdana;">str=str.Replace("abc","ABC");</span></div>