Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d57e5d5

Browse files
doc: update question 467 substr vs substring
1 parent a5cf154 commit d57e5d5

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

‎README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8845,8 +8845,19 @@ The execution context is created when a function is called. The function's code
88458845
**[⬆ Back to Top](#table-of-contents)**
88468846
88478847
467. ### What is the difference between substring and substr methods?
8848-
Both substring() and substr() are string methods, which are used to find substring of a given string. But there are some notable differences with their usage,
8849-
8848+
There are subtle differences between the substring() and substr() methods, so you should be careful not to get them confused.
8849+
8850+
- The two parameters of substr() are start and length, while for substring(), they are start and end.
8851+
- substr()'s start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0.
8852+
- Negative lengths in substr() are treated as zero, while substring() will swap the two indexes if end is less than start.
8853+
8854+
Furthermore, substr() is considered a legacy feature in ECMAScript, so it is best to avoid using it if possible.
8855+
8856+
```javascript
8857+
const text = "Mozilla";
8858+
console.log(text.substring(2, 5)); // "zil"
8859+
console.log(text.substr(2, 3)); // "zil"
8860+
```
88508861
88518862
**[⬆ Back to Top](#table-of-contents)**
88528863

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /