public void TestMethod1()
{
string str = "ABCDEFGHIJKLMN";
string result = str.Substring(2); //CDEFGHIJKLMN
string result1 = str.Substring(2, 3); //CDE
Console.ReadKey();
//string result2 = str.Substring();
}
將subString(index,length)第一個參數看做從1開始的索引,將index位置的之前的字符都給截掉,length表示從索引位置截取字段的長度,若不指定長度,則截取字段到字符串末尾。
length的最大長度為str.length-index!
--sql server 中substring()函數
select SUBSTRING('abcde',0,2);--a
select SUBSTRING('abcde',0,5);--abcd
select SUBSTRING('abcde',1,2);--ab
select SUBSTRING('abcde',-1,5);--abc
select SUBSTRING('abcdefghijk',3,5);--cdefg
SUBSTRING(str,start,length) :表示str數據源,str計數位置默認從1開始,截取的字段為從 start開始的 length個字符(包括start);字符串截取start到 start+length-1位置的字符,起始位置若為0,或者小於0,則進行補全再截取長度
實例:
select top 5 substring(ApprovalUser,charindex('',ApprovalUser)+1 ,
LEN(ApprovalUser)) as [Last Name] from CustomerBase