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);
          結果如下:
          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)  ——得到 new1
          str.Substring(str.LastIndexOf("."),str.Length-str.LastIndexOf(".") ——得到 .jpg

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

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

          導航

          統計

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 麻栗坡县| 房山区| 梁山县| 莱州市| 兴安县| 长子县| 许昌县| 依兰县| 桓台县| 毕节市| 开鲁县| 都匀市| 鹤壁市| 吉木萨尔县| 济南市| 曲松县| 台江县| 全州县| 龙南县| 阿鲁科尔沁旗| 菏泽市| 都昌县| 庄河市| 黄浦区| 土默特右旗| 宜川县| 金秀| 肇庆市| 靖宇县| 揭阳市| 西藏| 缙云县| 荃湾区| 乌兰察布市| 沁源县| 萍乡市| 武城县| 洪江市| 马尔康县| 原平市| 伊金霍洛旗|