Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

javascript - substr和substring有什么区别?(What is the difference between substr and substring?)

What is the difference between

(之间有什么区别)

alert("abc".substr(0,2));

and

(和)

alert("abc".substring(0,2));

They both seem to output “ab”.

(他们似乎都输出“ ab”。)

  ask by Difference Engine translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The difference is in the second argument.

(区别在于第二个参数。)

The second argument to substring is the index to stop at (but not include), but the second argument to substr is the maximum length to return.

(substring的第二个参数是要在(但不包括)处停止的索引,而substr的第二个参数是要返回的最大长度。)

Links?

(链接?)

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substr

(https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substr)

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substring

(https://developer.mozilla.org/zh-CN/JavaScript/Reference/Global_Objects/String/substring)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...