C# subString的理解

        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

原文地址:https://www.cnblogs.com/Unrmk-LingXing/p/4139517.html