substring、substr、limit

Java substring

在String中有两个substring()函数,如下:

一:String.substring(int start)
参数:
    start:要截取位置的索引

返回:
   从start开始到结束的字符串

例如:String str = "hello word!";
     System.out.println(str.substring(1));
     System.out.println(str.substring(3));
   System.out.println(str.substring(6));

将得到结果为:
         ello word!
         lo word!
         ord!
如果start大于字符串的长度将会抛出越界异常;

二:String.substring(int beginIndex, int endIndex)
参数:
  beginIndex 开始位置索引
   endIndex    结束位置索引

返回:
      从beginIndex位置到endIndex位置内的字符串(不含endIndex)

例如:String str = "hello word!";
     System.out.println(str.substring(1,4));
     System.out.println(str.substring(3,5));
   System.out.println(str.substring(0,4));

将得到结果为:
        ell
        lo 
        hell

如果startIndex和endIndex其中有越界的将会抛出越界异常。


JS substr

语法:string.substr(start,length) 第一个字符的索引是0,start必选 length为截取字符串长度,可选

substr(start,length) 返回从start位置开始length长度的子串
var str = "hello Tony";
str.substr(6);  //Tony
str.substring(6); //Tony

“goodboy”.substr(1,6); //oodboy 

MySQL limit
substr(start,length) 返回从start位置开始length长度的子串
SELECT * FROM table LIMIT [offset], rows | rows OFFSET offset

LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。
如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1): 为了与 PostgreSQL 兼容,MySQL 也支持句法: LIMIT # OFFSET #。

SELECT * FROM table LIMIT 5,10 //检索记录行 6-15

为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1:
SELECT * FROM table LIMIT 95,-1;
检索记录行 96-last.

如果只给定一个参数,它表示返回最大的记录行数目:
SELECT * FROM table LIMIT 5;

检索前 5 个记录行
换句话说,LIMIT n 等价于 LIMIT 0,n。



substring,substr,limit第一个参数都是从0开始;


substring,substr只有一个参数时,是截取从start到结束的所有值,limit一个参数n时,是取从0开始的前n条记录
substr,limit两个参数时,第二个参数是指length(rows)

猜你喜欢

转载自www.cnblogs.com/erichi101/p/12295105.html
今日推荐