標籤: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#中字串截取使用的方法