Created
October 28, 2014 08:49
-
-
Save z007/e53a39bc2e3b389918fc to your computer and use it in GitHub Desktop.
slice() substr() substring()区别
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>slice()</title> | |
<script type="text/javascript"> | |
/*slice() 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分。 | |
可为负 | |
start 要抽取的片断的起始下标。如果是负数,则该参数规定的是从字符串的尾部开始算起的位置。也就是说,-1 指字符串的最后一个字符,-2 指倒数第二个字符,以此类推。 | |
end 紧接着要抽取的片段的结尾的下标。若未指定此参数,则要提取的子串包括 start 到原字符串结尾的字符串。如果该参数是负数,那么它规定的是从字符串的尾部开始算起的位置。 | |
返回值 | |
一个新的字符串。包括字符串 stringObject 从 start 开始(包括 start)到 end 结束(不包括 end)为止的所有字符。 | |
说明 | |
String 对象的方法 slice()、substring() 和 substr() (不建议使用)都可返回字符串的指定部分。slice() 比 substring() 要灵活一些,因为它允许使用负数作为参数。slice() 与 substr() 有所不同,因为它用两个字符的位置来指定子串,而 substr() 则用字符位置和长度来指定子串 | |
*/ | |
var str="1234567890!" | |
document.write(str.slice(6)) | |
document.write("<br />") | |
document.write(str.slice(6,9)) | |
document.write("<br />") | |
document.write(str.slice(-2)) | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>surstr()</title> | |
<script type="text/javascript"> | |
/* | |
substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。 | |
语法 | |
stringObject.substr(start,length) | |
参数 描述 | |
start 必需。要抽取的子串的起始下标。必须是数值。如果是负数,那么该参数声明从字符串的尾部开始算起的位置。也就是说,-1 指字符串中最后一个字符,-2 指倒数第二个字符,以此类推。 | |
length 可选。子串中的字符数。必须是数值。如果省略了该参数,那么返回从 stringObject 的开始位置到结尾的字串。 | |
返回值 | |
一个新的字符串,包含从 stringObject 的 start(包括 start 所指的字符) 处开始的 length 个字符。如果没有指定 length,那么返回的字符串包含从 start 到 stringObject 的结尾的字符。 | |
*/ | |
var str="1234567!" | |
document.write(str.substr(3,7)+'<br/>') | |
document.write(str.substr(-1,7)+'<br/>') | |
document.write(str.substr(-1,2)+'<br/>') | |
document.write(str.substr(-1,1)+'<br/>') | |
document.write(str.substr(-2,1)+'<br/>') | |
document.write(str.substr(-2)+'<br/>') | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>surstring()</title> | |
<script type="text/javascript"> | |
var str="Hello world!" | |
document.write(str.substring(3)+'<br/>') | |
document.write(str.substring(3,7)+'<br/>') | |
document.write(str.substring(7,3)) | |
/* | |
substring() 方法用于提取字符串中介于两个指定下标之间的字符。 | |
语法 | |
stringObject.substring(start,stop) | |
参数 描述 | |
start 必需。一个非负的整数,规定要提取的子串的第一个字符在 stringObject 中的位置。 | |
stop | |
可选。一个非负的整数,比要提取的子串的最后一个字符在 stringObject 中的位置多 1。 | |
如果省略该参数,那么返回的子串会一直到字符串的结尾。 | |
返回值 | |
一个新的字符串,该字符串值包含 stringObject 的一个子字符串,其内容是从 start 处到 stop-1 处的所有字符,其长度为 stop 减 start。 | |
说明 | |
substring() 方法返回的子串包括 start 处的字符,但不包括 stop 处的字符。 | |
如果参数 start 与 stop 相等,那么该方法返回的就是一个空串(即长度为 0 的字符串)。如果 start 比 stop 大,那么该方法在提取子串之前会先交换这两个参数。 | |
提示和注释 | |
重要事项:与 slice() 和 substr() 方法不同的是,substring() 不接受负的参数。 | |
*/ | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment