|
1 |
| -# String Methods in JavaScript |
| 1 | +# String In JavaScript |
2 | 2 |
|
3 |
| -### charAt() |
4 |
| -```javascript |
5 |
| -const charAtIndex = "Hello".charAt(1); // Output: "e" |
6 |
| -``` |
| 3 | +<p align="center"> |
| 4 | + <a href="https://youtube.com/live/Unc365YFdf4"> |
| 5 | + <img src="https://img.youtube.com/vi/Unc365YFdf4/0.jpg" alt="String In JavaScript" /> |
| 6 | + </a> |
| 7 | +</p> |
7 | 8 |
|
8 |
| -### concat() |
| 9 | +### Length of a String |
9 | 10 | ```javascript
|
10 |
| -const concatenated = "Hello".concat(" World"); // Output: "Hello World" |
| 11 | +let firstName = "Vaishali"; |
| 12 | +console.log(firstName.length); |
11 | 13 | ```
|
12 | 14 |
|
13 |
| -### toUpperCase() |
| 15 | +### Access String Element |
14 | 16 | ```javascript
|
15 |
| -const upperCaseString = "hello".toUpperCase(); // Output: "HELLO" |
| 17 | +console.log(firstName.charAt(2)); // i |
| 18 | +console.log(firstName[2]); // i |
| 19 | +console.log(firstName.charCodeAt(2)); // 115 (Ascii Code) |
16 | 20 | ```
|
17 | 21 |
|
18 |
| -### toLowerCase() |
| 22 | +### Check Presence of Character |
19 | 23 | ```javascript
|
20 |
| -const lowerCaseString = "HeLLo".toLowerCase(); // Output: "hello" |
| 24 | +console.log(firstName.includes("r")); // false (& if present it return true) |
| 25 | +console.log(firstName.indexOf("i")); // 2 (& if not present it return -1) |
| 26 | +console.log(firstName.lastIndexOf("i")); // 7 |
21 | 27 | ```
|
22 | 28 |
|
23 |
| -### substring() |
| 29 | +### Compare Two Strings |
24 | 30 | ```javascript
|
25 |
| -const subString = "JavaScript".substring(4, 10); // Output: "Script" |
| 31 | +let anotherName = "Vishal"; |
| 32 | +console.log(firstName.localeCompare(anotherName)); // -1 (& if strings are equal it return 0) |
26 | 33 | ```
|
27 | 34 |
|
28 |
| -### replace() |
| 35 | +### Replace Substring |
29 | 36 | ```javascript
|
30 |
| -const replacedString = "Hello, name!".replace("name", "John"); // Output: "Hello, John!" |
| 37 | +const str = "Vishal is Best Frontend Developer. Vishal is Best Developer. "; |
| 38 | +console.log(str.replace("Vishal", "Sujit")); // "Sujit is Best Frontend Developer. Vishal is Best Developer. " |
| 39 | +console.log(str.replaceAll("Vishal", "Sujit")); // "Sujit is Best Frontend Developer. Sujit is Best Developer. " |
31 | 40 | ```
|
32 | 41 |
|
33 |
| -### split() |
| 42 | +### Substring of a String |
34 | 43 | ```javascript
|
35 |
| -const splitArray = "apple,banana,grape".split(","); // Output: ["apple", "banana", "grape"] |
| 44 | +console.log(str.substring(6, 30)); |
| 45 | +console.log(str.slice(-10, -1)); |
36 | 46 | ```
|
37 | 47 |
|
38 |
| -### indexOf() |
| 48 | +### Split and Join |
39 | 49 | ```javascript
|
40 |
| -const index = "JavaScript".indexOf("Script"); // Output: 4 |
| 50 | +console.log(str.split("")); |
| 51 | +const subString = str.split(" "); |
| 52 | +console.log(subString.join(" ")); |
41 | 53 | ```
|
42 | 54 |
|
43 |
| -### endsWith() |
| 55 | +### String Start and End |
44 | 56 | ```javascript
|
45 |
| -const endsWithWorld = "Hello, world".endsWith("world"); // Output: true |
| 57 | +console.log(str.startsWith("Vishal")); // true |
| 58 | +console.log(str.endsWith("Developer")); // true |
46 | 59 | ```
|
47 | 60 |
|
48 |
| -### startsWith() |
| 61 | +### Trim and Case Conversion |
49 | 62 | ```javascript
|
50 |
| -const startsWithHello = "Hello, world".startsWith("Hello"); // Output: true |
| 63 | +const trimStr = str.trim(); |
| 64 | +const trimStrStart = str.trimStart(); |
| 65 | +const trimStrEnd = str.trimEnd(); |
| 66 | +console.log(trimStr, trimStr.length); |
| 67 | +console.log(str.toLowerCase()); |
| 68 | +console.log(str.toUpperCase()); |
51 | 69 | ```
|
52 | 70 |
|
53 |
| -### includes() |
| 71 | +### Convert Number and Object to String |
54 | 72 | ```javascript
|
55 |
| -const includesWorld = "Hello, world".includes("world"); // Output: true |
| 73 | +const num = 123; |
| 74 | +console.log(num, num.toString()); |
| 75 | + |
| 76 | +const obj = { |
| 77 | + name: "Vishal", |
| 78 | + course: "DSA with Vishal" |
| 79 | +}; |
| 80 | +console.log(obj, JSON.stringify(obj)); |
56 | 81 | ```
|
57 | 82 |
|
58 |
| -### trim() |
| 83 | +### Concatenate Strings |
59 | 84 | ```javascript
|
60 |
| -const trimmedString = " Hello, world ".trim(); // Output: "Hello, world" |
| 85 | +const lastName = "Rajput"; |
| 86 | +console.log(firstName + lastName); |
| 87 | +console.log(`${firstName} ${lastName} is a Best Developer`); |
| 88 | +console.log(firstName.concat(lastName, " is a", " Best")); |
61 | 89 | ```
|
62 | 90 |
|
63 | 91 | ## Practice Questions
|
|
0 commit comments