You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/javascript/concepts/substring/substring.md
+14-12Lines changed: 14 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
Title: 'Substring'
3
-
Description: 'The .substring() method returns part of a string. If given two arguments, they are the start and end indexes of the characters returned. If given one argument, it returns characters from that point to the end of the string. javascript // Returns characters from startIndex to end of string string.substring(startIndex); // Returns characters from startIndex to endIndex string.substring(startIndex, endIndex); '
3
+
Description: 'Extracts a portion of a string between two given indices and returns a new string. If only one index is given, it goes to the end.'
4
4
Subjects:
5
5
- 'Web Development'
6
6
- 'Computer Science'
@@ -13,39 +13,41 @@ CatalogContent:
13
13
- 'paths/create-a-back-end-app-with-javascript'
14
14
---
15
15
16
-
The `.substring()` method returns part of a string. If given two arguments, they are the start and end indexes of the characters returned. If given one argument, it returns characters from that point to the end of the string.
16
+
The **`substring()`** method in JavaScript extracts a portion of a string from one position to another (exclusive) and returns a new string. If the second position is omitted, it returns characters from the first position to the end of the string.
17
17
18
18
## Syntax
19
19
20
-
```javascript
20
+
```pseudo
21
21
// Returns characters from startIndex to end of string
22
22
string.substring(startIndex);
23
+
```
24
+
25
+
Or alternatively:
23
26
27
+
```pseudo
24
28
// Returns characters from startIndex to endIndex
25
29
string.substring(startIndex, endIndex);
26
30
```
27
31
28
32
## Details
29
33
30
-
-`.substring()` returns characters at the start index up to, but not including, the character at the end index.
31
-
32
-
- If the end index is omitted `.substring()` returns characters at the start index up through the end of the string.
34
+
-`.substring()` returns characters from the start index up to, but not including, the character at the end index.
33
35
34
-
- If the start and end indexes are equal, `.substring()` returns an empty string.
36
+
- If the end index is omitted, `.substring()` returns characters from the start index through the end of the string.
35
37
36
-
-Indexes less than zero are interpreted as zero.
38
+
-If the start and end indices are equal, `.substring()` returns an empty string.
37
39
38
-
-Indexes that are `NaN` are treated as zero.
40
+
-Indices that are less than zero or are `NaN` are interpreted as zero.
39
41
40
-
-Indexes that are greater than `string.length` are treated as `string.length`.
42
+
-Indices that are greater than `string.length` are treated as `string.length`.
41
43
42
44
- If the first argument is greater than the second argument, the first argument is treated as the end index and the second argument is treated as the start index.
43
45
44
46
## Example 1
45
47
46
48
Using `.substring()` to display characters from a given string.
0 commit comments