|
1 | | -// INFO: String Methods & Properties |
2 | | -// Before starting string methods and properties we need to know that strings are indexed. So basically we can access only single character of a string by doing something like this. |
| 1 | +// strings_methods&properties.js |
3 | 2 |
|
4 | | -// Expression: "Hello World"[1] |
5 | | -// Result: "e" |
| 3 | +// INFO: String Methods & Properties in JavaScript |
6 | 4 |
|
7 | | -let message = "Hello World"; |
8 | | -let message1 = "Hello"; |
9 | | -let number = '1234'; |
10 | | -console.log(message[4]); |
| 5 | +/* |
| 6 | +Strings in JavaScript are sequences of characters and are indexed starting from 0. |
| 7 | +This means you can access individual characters using bracket notation. |
| 8 | +
|
| 9 | +Example: |
| 10 | +"Hello World"[1] // returns "e" |
| 11 | +*/ |
11 | 12 |
|
| 13 | +// Accessing characters by index |
| 14 | +let message = "Hello World"; |
| 15 | +console.log(message[4]); // Output: o |
12 | 16 |
|
13 | 17 | // Properties |
14 | | -// NOTE: There is only one property for string (which is length) |
15 | | -console.log(message.length); |
| 18 | +// NOTE: The main property of strings is 'length' which returns the number of characters in the string. |
| 19 | +console.log(message.length); // Output: 11 |
| 20 | + |
| 21 | +// Methods without arguments |
| 22 | + |
| 23 | +// Convert to uppercase |
| 24 | +console.log(message.toUpperCase()); // Output: "HELLO WORLD" |
16 | 25 |
|
17 | | -// Methods without argument |
18 | | -console.log(message.toUpperCase()); |
19 | | -console.log(message.toLowerCase()); |
| 26 | +// Convert to lowercase |
| 27 | +console.log(message.toLowerCase()); // Output: "hello world" |
20 | 28 |
|
| 29 | +// Remove whitespace from both ends |
21 | 30 | let newMessage = " hi this is me . "; |
22 | | -console.log(newMessage.trim()); |
23 | | -console.log(newMessage.trimStart()); |
24 | | -console.log(newMessage.trimEnd()); |
25 | | - |
26 | | -// Methods with argument |
27 | | -console.log(message.includes("Hello")); |
28 | | -console.log(message.indexOf("e")); |
29 | | -console.log(message.replace('Hello', 'Hi')); |
30 | | -console.log(message1.replaceAll('l', 'L')); |
31 | | -console.log(message.concat(' ' + message1)); |
32 | | -console.log(number.padStart(11, '*')); |
33 | | -console.log(number.padEnd(12, "-")); |
34 | | -console.log(message.charAt(1)); |
35 | | -console.log(message.charCodeAt(0)); |
36 | | -console.log(message.split(' ')); |
| 31 | +console.log(newMessage.trim()); // Output: "hi this is me ." |
| 32 | +console.log(newMessage.trimStart()); // Removes only the starting spaces |
| 33 | +console.log(newMessage.trimEnd()); // Removes only the ending spaces |
| 34 | + |
| 35 | +// Methods with arguments |
| 36 | + |
| 37 | +// Check if string includes substring |
| 38 | +console.log(message.includes("Hello")); // Output: true |
| 39 | + |
| 40 | +// Get index of first occurrence of character/string |
| 41 | +console.log(message.indexOf("e")); // Output: 1 |
| 42 | + |
| 43 | +// Replace first occurrence of substring |
| 44 | +console.log(message.replace("Hello", "Hi")); // Output: "Hi World" |
| 45 | + |
| 46 | +// Replace all occurrences (ES2021 feature) |
| 47 | +console.log("Hello Hello".replaceAll("l", "L")); // Output: "HeLLo HeLLo" |
37 | 48 |
|
| 49 | +// Concatenate strings |
| 50 | +console.log(message.concat(" " + "Everyone")); // Output: "Hello World Everyone" |
| 51 | + |
| 52 | +// Pad string from start to reach a certain length |
| 53 | +let number = "1234"; |
| 54 | +console.log(number.padStart(11, "*")); // Output: *******1234 |
| 55 | + |
| 56 | +// Pad string from end to reach a certain length |
| 57 | +console.log(number.padEnd(12, "-")); // Output: 1234-------- |
| 58 | + |
| 59 | +// Get character at specific index |
| 60 | +console.log(message.charAt(1)); // Output: "e" |
| 61 | + |
| 62 | +// Get Unicode value of character at specific index |
| 63 | +console.log(message.charCodeAt(0)); // Output: 72 (Unicode for 'H') |
| 64 | + |
| 65 | +// Split string into array based on separator |
| 66 | +console.log(message.split(" ")); // Output: [ 'Hello', 'World' ] |
38 | 67 |
|
39 | 68 | // String Template Literals |
40 | | -const templateString = 17; |
41 | | -console.log(`I am ${templateString} years old`); |
| 69 | +const age = 17; |
| 70 | +console.log(`I am ${age} years old`); // Output: "I am 17 years old" |
| 71 | + |
| 72 | +// Multi-line strings using template literals |
| 73 | +const multiLine = ` |
| 74 | +This is line 1 |
| 75 | +This is line 2 |
| 76 | +This is line 3 |
| 77 | +`; |
| 78 | +console.log(multiLine); |
| 79 | + |
| 80 | +/* |
| 81 | +Summary: |
| 82 | +
|
| 83 | +Property: |
| 84 | +- length |
| 85 | +
|
| 86 | +Methods (no argument): |
| 87 | +- toUpperCase() |
| 88 | +- toLowerCase() |
| 89 | +- trim() |
| 90 | +- trimStart() |
| 91 | +- trimEnd() |
| 92 | +
|
| 93 | +Methods (with argument): |
| 94 | +- includes(substring) |
| 95 | +- indexOf(substring) |
| 96 | +- replace(searchValue, newValue) |
| 97 | +- replaceAll(searchValue, newValue) |
| 98 | +- concat(string) |
| 99 | +- padStart(targetLength, padString) |
| 100 | +- padEnd(targetLength, padString) |
| 101 | +- charAt(index) |
| 102 | +- charCodeAt(index) |
| 103 | +- split(separator) |
| 104 | +- Template Literals with backticks for interpolation and multi-line strings |
| 105 | +*/ |
0 commit comments