c#中字串截取使用的方法

來源:互聯網
上載者:User

標籤:winform   blog   http   os   使用   ar   java   strong   for   

AndyZhangwelcome to java worldc#中字串截取使用的方法String substring(int beginIndex) 
String substring(int beginIndex, int endIndex) 
String.Substring (Int32)         子字串從指定的字元位置開始。 
String.Substring (Int32, Int32) 子字串從指定的字元位置開始且具有指定的長度。
舉例如下:
             string s = "Hello C# World!";
             //s1為從s中截取的位置為3的字元以後的字元子串,3表示子字串的起始字元位置
            string s1=s.Substring(3);
             //s2為從s中截取的位置為6的字元開始長度為2的字串,6表示子字元的起始字元位置,2表示子字元長度
            string s2 = s.Substring(6, 2);
結果如下:
lo C# World!
C# int indexOf(String str) 返回第一次出現的指定子字串在此字串中的索引。
int indexOf(String str, int fromIndex) 從指定的索引處開始,返回第一次出現的指定子字串在此字串中的索引。int lastIndexOf(String str) 返回在此字串中最右邊出現的指定子字串的索引。 
int lastIndexOf(String str, int fromIndex) 從指定的索引處開始向後搜尋,返回在此字串中最後一次出現的指定子字串的索引。 
int length() 返回此字串的長度。boolean startsWith(String prefix) 測試此字串是否以指定的首碼開始。 
boolean startsWith(String prefix, int toffset) 測試此字串是否以指定首碼開始,該首碼以指定索引開始。
例如:
  string  str="C:\\Documents and Settings\\Administrator\\案頭\\new1.jpg" 
str.Substring(0,str.LastIndexOf("\\")+1)+"new"+str.Substring(str.LastIndexOf("\\")+1,
  str.LastIndexOf(".")-str.LastIndexOf("\\")-1)+str.Substring(str.LastIndexOf("."),str.Length-str.LastIndexOf(".")  str.LastIndexOf("\\")——得到最後一個“\\”的索引值  str.Substring(0,str.LastIndexOf("\\")+1)——得到  C:\\Documents and Settings\\Administrator\\案頭\\ str.Substring(str.LastIndexOf("\\")+1,str.LastIndexOf(".")-str.LastIndexOf("\\")-1)  ——得到 new1str.Substring(str.LastIndexOf("."),str.Length-str.LastIndexOf(".") ——得到 .jpg

posted on 2012-04-25 17:19 SkyDream 閱讀(42180) 評論(0)  編輯  收藏 所屬分類: C# WinForm

c#中字串截取使用的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.