Skip to content

Commit 4169096

Browse files
authored
Merge pull request #298 from mauroaccornero/update-question-467
doc: update question 467 substr vs substring
2 parents a839cd1 + d57e5d5 commit 4169096

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -8846,8 +8846,19 @@ The execution context is created when a function is called. The function's code
88468846
**[⬆ Back to Top](#table-of-contents)**
88478847
88488848
467. ### What is the difference between substring and substr methods?
8849-
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,
8850-
8849+
There are subtle differences between the substring() and substr() methods, so you should be careful not to get them confused.
8850+
8851+
- The two parameters of substr() are start and length, while for substring(), they are start and end.
8852+
- substr()'s start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0.
8853+
- Negative lengths in substr() are treated as zero, while substring() will swap the two indexes if end is less than start.
8854+
8855+
Furthermore, substr() is considered a legacy feature in ECMAScript, so it is best to avoid using it if possible.
8856+
8857+
```javascript
8858+
const text = "Mozilla";
8859+
console.log(text.substring(2, 5)); // "zil"
8860+
console.log(text.substr(2, 3)); // "zil"
8861+
```
88518862
88528863
**[⬆ Back to Top](#table-of-contents)**
88538864

0 commit comments

Comments
 (0)