|
2 | 2 |
|
3 | 3 | There are many methods that we can use against strings. Here's a list of a few of them:
|
4 | 4 |
|
5 |
| -```js |
6 |
| -// .length() |
7 |
| -const str = 'hello' |
8 |
| -str.length() // 5 |
9 |
| -// .indexOf() |
10 |
| -const str2 = 'this is a short sentence' |
11 |
| -str2.indexOf('short'); // 10 |
12 |
| -// slice() |
13 |
| -const str3 = 'pizza, orange, cereals' |
14 |
| -str3.slice(0,5) // 'pizza' |
15 |
| -// toUppercase() / toLowerCase() |
16 |
| -const str4 = 'lowercase' |
17 |
| -str4.toUpperCase(); // 'LOWERCASE' |
18 |
| -const str5 = 'UPPERCASE' |
19 |
| -str5.toLowerCase() // 'uppercase' |
20 |
| -``` |
21 |
| - |
22 |
| -There are many more methods, these were just a few as a reminder. |
| 5 | + 1. **indexOf()** |
| 6 | + |
| 7 | + Gets the position of the first occurence of the specified value in a string. |
| 8 | + ```js |
| 9 | + const str = "this is a short sentence"; |
| 10 | + str.indexOf("short"); |
| 11 | + // Output: 10 |
| 12 | + ``` |
| 13 | + 2. **slice()** |
| 14 | + |
| 15 | + Pulls a specified part of a string as a new string. |
| 16 | + ```js |
| 17 | + const str = "pizza, orange, cereals" |
| 18 | + str.slice(0, 5); |
| 19 | + // Output: "pizza" |
| 20 | + ``` |
| 21 | + 3. **toUpperCase()** |
| 22 | + |
| 23 | + Turns all characters of a string to uppercase. |
| 24 | + ```js |
| 25 | + const str = "i ate an apple" |
| 26 | + str.toUpperCase() |
| 27 | + // Output: "I ATE AN APPLE" |
| 28 | + ``` |
| 29 | + 4. **toLowerCase()** |
| 30 | + |
| 31 | + Turns all characters of a string to lowercase. |
| 32 | + ``` |
| 33 | + const str = "I ATE AN APPLE" |
| 34 | + str.toLowerCase() |
| 35 | + // Output: "i ate an apple" |
| 36 | + ``` |
| 37 | + |
| 38 | +There are many more methods, these were just a few as a reminder. Check the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#) for a more in-depth description on the above methods. |
23 | 39 |
|
24 | 40 |
|
25 | 41 |
|
@@ -112,4 +128,4 @@ As the name suggests, this new method will repeat what we pass in.
|
112 | 128 | let hello = "Hi";
|
113 | 129 | console.log(hello.repeat(10));
|
114 | 130 | // "HiHiHiHiHiHiHiHiHiHi"
|
115 |
| -``` |
| 131 | +``` |
0 commit comments