方法1 Substring(Int32) 從此實例檢索子字符串。 子字符串在指定的字符位置開始並一直到該字符串的末尾。
方法2 Substring(Int32, Int32) 從此實例檢索子字符串。 子字符串從指定的字符位置開始且具有指定的長度。
參數一:起始位置(從0開始)
參數二:指定長度
用法:string變量名.Substring(參數一, 參數二);
舉例:
string s = "hello world";
string ss;
例子1 //從指定位置開始到結尾的字符串(0位開始)
int i=1;
ss = s.Substring(i);
ss = "ello world"
例子2 //從指定位置開始取固定長度的字符串
int i=1;
ss = s.Substring(i,3);
ss = "ell"
例子3 //返回左邊的i個字符
int i=5;
ss = s.Substring(0,i)
ss = "hello"
例子4 //返回右邊的i個字符
int i=5;
ss = s.Substring(s.Length-i,i);
ss = "world"
例子5 //返回兩個特定字符之間的字符串
int IndexofA = s.IndexOf('e'); //字符串的話總以第一位為指定位置
int IndexofB = s.IndexOf('r');
ss = s.Substring(IndexofA + 1, IndexofB - IndexofA -1);
ss = "llo wo"