cnblogs/dcrenl/c#.net常见字符串处理方法(1).html
2024-09-24 12:43:01 +08:00

100 lines
4.7 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div id="sina_keyword_ad_area2" class="articalContent ">
<div style="text-indent: 2em;">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 style="text-indent: 2em;">
----------------------------------------------------------------------------------------</div>
<div style="text-indent: 2em;">
<h1>.net中几个经常用到的字符串的截取</h1>
</div>
<div style="text-indent: 2em;"><span style="line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">string str="123abc456";<br />
int i=3;<br />
1 取字符串的前i个字符<br />
&nbsp;&nbsp;&nbsp;<span style="font-family: Verdana;"><span style="color: #008000; font-size: 13px;">str=str.Substring(0,i); //
or&nbsp;&nbsp;str=str.Remove(i,str.Length-i);&nbsp;</span><br />
2 去掉字符串的前i个字符<br />
&nbsp;&nbsp;&nbsp;<span style="color: #008000; font-size: 13px;">str=str.Remove(0,i);&nbsp;
// or str=str.Substring(i);&nbsp;</span><br />
3 从右边开始取i个字符<br />
&nbsp;&nbsp;</span><span style="font-family: Verdana;"><span style="color: #008000; font-size: 13px;">str=str.Substring(str.Length-i);
// or str=str.Remove(0,str.Length-i);<br /></span>4
从右边开始去掉i个字符<br />
&nbsp;&nbsp;&nbsp;<span style="color: #008000; font-size: 13px;">str=str.Substring(0,str.Length-i);
// or str=str.Remove(str.Length-i,i);</span><br />
5 判断字符串中是否有"abc" 有则去掉之<br />
&nbsp;&nbsp;&nbsp;<span style="color: red; font-size: 13px;">using
System.Text.RegularExpressions;</span><br /></span><span style="color: #008000; font-size: 13px;"><span style="font-family: Verdana;">&nbsp;&nbsp;&nbsp;string
str = "123abc456";<br />
&nbsp;&nbsp;&nbsp;string
a="abc";<br />
&nbsp;&nbsp; Regex r =
new&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;"><span style="color: #0000ff; font-size: 13px;">//绿色部分与紫色部分取一种即可。<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str=str.Replace(a,"");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; font-size: 13px;">Response.Write(str);&nbsp;&nbsp;&nbsp;</span><br />
</span><span style="font-family: Verdana;"><span style="color: #ff00ff; font-size: 13px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
string str1,str2;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
str1=str.Substring(0,m.Index);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
str2=str.Substring(m.Index+a.Length,str.Length-a.Length-m.Index);</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #ff00ff; font-size: 13px;">Response.Write(str1+str2);&nbsp;</span><br />
&nbsp;&nbsp;&nbsp;}</span></span><br />
6 如果字符串中有"abc"则替换成"ABC"<br />
&nbsp;&nbsp;&nbsp;<span style="color: #008000; font-size: 13px;"><span style="font-family: Verdana;">str=str.Replace("abc","ABC");</span></span></span></div>
</div>
<p>&nbsp;</p>