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

          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);
          結(jié)果如下:
          lo C# World!
          C#

          int indexOf(String str) 返回第一次出現(xiàn)的指定子字符串在此字符串中的索引。
          int indexOf(String str, int fromIndex) 從指定的索引處開始,返回第一次出現(xiàn)的指定子字符串在此字符串中的索引。
          int lastIndexOf(String str) 返回在此字符串中最右邊出現(xiàn)的指定子字符串的索引。 
          int lastIndexOf(String str, int fromIndex) 從指定的索引處開始向后搜索,返回在此字符串中最后一次出現(xiàn)的指定子字符串的索引。 
          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)  ——得到 new1
          str.Substring(str.LastIndexOf("."),str.Length-str.LastIndexOf(".") ——得到 .jpg

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

          <2012年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 益阳市| 公安县| 武义县| 东山县| 石景山区| 泾阳县| 洛浦县| 万年县| 绥江县| 嘉鱼县| 淮滨县| 合肥市| 景谷| 留坝县| 射洪县| 南安市| 潼南县| 伊金霍洛旗| 台湾省| 永川市| 黔西| 临安市| 开阳县| 伊宁市| 徐汇区| 万盛区| 疏勒县| 六安市| 江永县| 永德县| 越西县| 安化县| 弋阳县| 浦城县| 金秀| 舟曲县| 新津县| 江阴市| 克什克腾旗| 北安市| 武威市|