100 lines
4.7 KiB
HTML
100 lines
4.7 KiB
HTML
<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'); //无第二参数为加空格,结果为“100”<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 />
|
||
} </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 />
|
||
<span style="font-family: Verdana;"><span style="color: #008000; font-size: 13px;">str=str.Substring(0,i); //
|
||
or str=str.Remove(i,str.Length-i); </span><br />
|
||
|
||
2 去掉字符串的前i个字符:<br />
|
||
<span style="color: #008000; font-size: 13px;">str=str.Remove(0,i);
|
||
// or str=str.Substring(i); </span><br />
|
||
3 从右边开始取i个字符:<br />
|
||
</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 />
|
||
<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 />
|
||
<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;"> string
|
||
str = "123abc456";<br />
|
||
string
|
||
a="abc";<br />
|
||
Regex r =
|
||
new Regex(a); <br />
|
||
Match m =
|
||
r.Match(str); <br />
|
||
if
|
||
(m.Success)<br />
|
||
{<br />
|
||
</span><span style="font-family: Verdana;"><span style="color: #0000ff; font-size: 13px;">//绿色部分与紫色部分取一种即可。<br />
|
||
</span> str=str.Replace(a,"");<br />
|
||
|
||
<span style="color: #008000; font-size: 13px;">Response.Write(str); </span><br />
|
||
</span><span style="font-family: Verdana;"><span style="color: #ff00ff; font-size: 13px;">
|
||
string str1,str2;<br />
|
||
|
||
str1=str.Substring(0,m.Index);<br />
|
||
|
||
str2=str.Substring(m.Index+a.Length,str.Length-a.Length-m.Index);</span><br />
|
||
|
||
<span style="color: #ff00ff; font-size: 13px;">Response.Write(str1+str2); </span><br />
|
||
|
||
}</span></span><br />
|
||
|
||
6 如果字符串中有"abc"则替换成"ABC"<br />
|
||
<span style="color: #008000; font-size: 13px;"><span style="font-family: Verdana;">str=str.Replace("abc","ABC");</span></span></span></div>
|
||
|
||
|
||
</div>
|
||
<p> </p> |