標籤:style io ar for sp div on art ad
1.字串比較
字串.ComparTo(目標字串)
"a".ComparTo("b");
2.尋找子串
字串.IndexOf(子串,尋找其實位置) ;
字串.LastIndexOf(子串) ;最後一次出現的位置
str.IndexOf("ab",0);
3.插入子串
字串.Insert(插入位置,插入子串) ;
s.Insert(2,"ab");
4.移出子串
字串.Remove(其實位置,移出數);
s.Remove(3,2);
5.替換子串
字串.Replace(源子串,替換為);
s.Replace("-"," ");將-替換為空白格
6.截取子串
字串.Substring(截取其實位置,截取個數);
"abcdef".Substring(2,3);結果為cde
7.去空格
myString = myString.Trim(); //同時刪除字串前後的空格
char[] trimChars = {‘ ‘,‘e‘,‘s‘}; //準備刪除的字元
myString = myString.Trim(trimChars); //刪除所有指定字元
myString = myString.TrimEnd(); //刪除字串後的空格
myString = myString.TrimStart(); //刪除字串前的空格
8.轉換大小寫
str.ToLower()轉化成小寫字母
str.ToUpper()轉化成大寫字母
Char.IsUpper(str,3)//判斷一個字串中的第n個字元是否是大寫
9.拆分字串
string[] Strs = myString.Split(‘ ‘,3); //按照空格進行拆分,並且返回前三個字串,結果在字串數組裡
10.使字串達到指定長度
PadLeft()、PadRight() ,如:
string str1="00";
str1=str1.PadLeft(3,‘1‘); //無第二參數為加空格,結果為“100”
11.得到字串長度
len=str.Length;
12.字串遍曆
string myString = "This is a test!";
foreach (char myChar in myString)
{
Console.Write("{0}",myChar);
} ----------------------------------------------------------------------------------------.net中幾個經常用到的字串的截取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");
c#.net常見字串處理方法