asp.net 操作字串

來源:互聯網
上載者:User
IndexOf-搜尋函數 int String.IndexOf(string value); 返回字串中第一次出現子字串的字元位置,從0開始,未找到子字串返回-1。 int String.IndexOf(string value,int startIndex); 同上,但是從字串的startIndex位置開始搜尋,一直到字串末尾。 int String.IndexOf(string value,int startIndex,int count); 同上,但是從字串的指定位置開始,並向後搜尋count個字元。 注意後兩個參數的取值要滿足下麵條件:0<=startIndex0樣本:string mystr="ABCABCABC"; 運算式 傳回值 mystr.IndexOf("a") -1 //大小寫敏感 mystr.IndexOf("A") 0 mystr.IndexOf("A",0) 0 //第一個字元的索引號為0 mystr.IndexOf("A",1) 3 mystr.IndexOf("A",1,2) -1 mystr.IndexOf("A",1,3) 3 //總共搜尋count個字元 LastIndexOf-反向搜尋函數 返回字串中子字串的最後出現的字元位置,從後向前找。 0<=startIndex0Insert-插入函數 string Insert(int startIndex,string value); 在字串的指定索引位置插入一個字串。在返回的字串中,value字串的第一個字元的索引號為startIndex。 Remove-刪除函數 string Remove(int startIndex,int count); 從字串的指定索引位置刪除指定數目的字元。索引和計數必須引用該字串內的位置,startIndex+count<=Length Replace-替換函數 string Replace(char oldChar,char newChar); string Replace(string oldValue,string newValue); Concat-串連函數 string Concat(參數); 參數可以是對象、對象數組、字串、字串數組,多個對象(字串)間以逗號分隔 Join-串聯函數 string Join(string separator,string[] value); 在字串數組的每個元素之間串聯指定的分隔字元,從而產生單個串聯的字串。 string Join(string separator,string[] value,int startIndex,int count); 串聯部分數組元素,從第startIndex個元素開始,串聯count個元素。 Split-分割函數 string[] Split(params char[] separator); 根據分割字元將字串分割成子字串,然後將結果作為字串數組返回。 string[] Split(char[] separator,int count); 參數count指定返回的最大數組元素數,進行部分分割 樣本:string mystr="1+2+3-4+5+6"; 運算式 返回 mystr.Split('-'); {"1+2+3","4+5+6"} mystr.Split("+-".ToCharArray()); {"1","2","3","4","5","6"} mystr.Split(new char[]{'+','-'}); {"1","2","3","4","5","6"} mystr.Split("+-".ToCharArray(),4); {"1","2","3","4+5+6"} mystr.Split("+-".ToCharArray(),100); {"1","2","3","4","5","6"} 注意上面分隔字元的幾種用法部分分割時,最多返回count個元素。 ToCharArray-打散函數
char[] ToCharArray(); 將字串中的字元複製到字元數組。 char[] ToCharArray(int startIndex,int length); 將字串中的指定子字串內的字元複製到字元數組。 樣本:string mystr="Diffmaker"; 運算式 返回 mystr.ToCharArray(); {'D','i','f','f','m','a','k','e','r'} mystr.ToCharArray(4,4); {'m','a','k','e'} Trim|TrimStart|TrimEnd-修剪函數 string Trim();//移除字串首尾空白字元(包括中英文空格) string TrimStart();//移除字串首部空白字元(包括中英文空格) string TrimEnd();//移除字串尾部空白字元(包括中英文空格) string Trim(params char[] trimChars);//移除字串首尾字元 string TrimStart(params char[] trimChars);//移除字串首部字元 string TrimEnd(params char[] trimChars);//移除字串尾部字元 當不指定參數時,移除的是空白字元當指定參數時,移除的是指定字元 StartsWith|EndsWith-端點函數 bool StartsWith(string value);//檢測字串是否以子串開始 bool EndsWith(string value);//檢測字串是否以子串結束 PadLeft|PadRight-填充函數 string PadLeft(int totalWidth);//在字串左側添加空格,使其達到指定長度 string PadRight(int totalWidth);//在字串右側添加空格,使其達到指定長度 string PadLeft(int totalWidth,char paddingChar);//左側添加指定字元到定長 string PadRight(int totalWidth,char paddingChar);//右側添加指定字元到定長 Substring-取子函數 string Substring(int startIndex);//從指定的字元位置開始至串尾 string Substring(int startIndex,int length);//從指定的字元位置開始取指定長度 startIndex 從零開始如果startIndex等於字串的長度,則返回:string.EmptystartIndex+count<=Length 其他簡單函數 String.ToLower();//轉小寫函數 String.ToUpper();//轉大寫函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.