<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>slice substring</title> </head> <body> <script> // 1.slice() 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分。 // stringObject.slice(start,end) // start 要抽取的片断的起始下标。如果是负数,则该参数规定的是从字符串的尾部开始算起的位置。也就是说,-1 指字符串的最后一个字符,-2 指倒数第二个字符,以此类推。 // end 紧接着要抽取的片段的结尾的下标。若未指定此参数,则要提取的子串包括 start 到原字符串结尾的字符串。如果该参数是负数,那么它规定的是从字符串的尾部开始算起的位置。 str = "Hello happy world!"; console.log(str.slice(6)); // happy world! console.log(str.slice(6,11)); // happy // 2.substring() 方法用于提取字符串中介于两个指定下标之间的字符。 // stringObject.substring(start,stop) // start 必需。一个非负的整数 // stop 可选。一个非负的整数 // 重要事项:与 slice() 方法不同的是,substring() 不接受负的参数。 var str = "Hello world!"; console.log(str.substring(3)); // lo world! console.log(str.substring(3).length == str.length - 3); // true console.log(str); // Hello world! console.log((str.substring(3,7))); // lo w // 返回字符串的长度7-3=4。 </script> </body> </html>
js slice substring.html
猜你喜欢
转载自blog.csdn.net/weixin_42193179/article/details/87191382
今日推荐
周排行