admin管理员组文章数量:1291187
In this program, I understand (I think) that paragraph.charAT(0) = "%" checks whether the first character in paragraph is equal to %, i.e. the counting starts at 0, so charAT(0) is the first character
However, in the line, paragraph.slice(1), what does the 1 refer to? Is it slicing off the first character?, which in this case will be at 0 position?
function processParagraph(paragraph) {
var header = 0;
while (paragraph.charAt(0) == "%") {
paragraph = paragraph.slice(1);
header++;
}
return {type: (header == 0 ? "p" : "h" + header),
content: paragraph};
}
show(processParagraph(paragraphs[0]));
In this program, I understand (I think) that paragraph.charAT(0) = "%" checks whether the first character in paragraph is equal to %, i.e. the counting starts at 0, so charAT(0) is the first character
However, in the line, paragraph.slice(1), what does the 1 refer to? Is it slicing off the first character?, which in this case will be at 0 position?
function processParagraph(paragraph) {
var header = 0;
while (paragraph.charAt(0) == "%") {
paragraph = paragraph.slice(1);
header++;
}
return {type: (header == 0 ? "p" : "h" + header),
content: paragraph};
}
show(processParagraph(paragraphs[0]));
Share
Improve this question
asked Feb 25, 2011 at 3:02
mjmitchemjmitche
2,0676 gold badges25 silver badges31 bronze badges
2
- 1 see here. MDC is a great resource for javascript. – aaronasterling Commented Feb 25, 2011 at 3:07
- See also What is the difference between String.slice and String.substring in JavaScript? – Rudu Commented Feb 25, 2011 at 3:14
4 Answers
Reset to default 9It extracts a substring starting at index 1 (2nd character) of the paragraph string.
For example, consider this:
var paragraph = "Hi my name is Russell";
console.log( paragraph.slice(1) ); //returns 'i my name is Russell'
.slice
string.slice(beginslice[, endSlice])
Extracts a section of a string and returns a new string.
It returns everything after the first character, essentially cutting the first character off.
It removes the first character from the string and returns that without altering the original string. I remend you look at the documentation for slice
.
it's slicing off the first character (which is a "%")
本文标签: javascriptwhat does the 1 in slice(1) refer to in this programStack Overflow
版权声明:本文标题:Javascript, what does the 1 in slice(1) refer to in this program? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741005792a2322844.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论