-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_Strings.js
More file actions
71 lines (49 loc) · 2.13 KB
/
Copy path04_Strings.js
File metadata and controls
71 lines (49 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// String are Immutable In JavaScript, which means that once a string is created, it cannot be changed. However, you can create a new string based on the original string.
let name = "Ritik";
// String length
console.log(name.length);
// String indexing
for(let i = 0; i < name.length; i++) {
console.log(name[i] + " is at index " + i);
}
// String iteration using for-of loop
for(let char of name) {
console.log(char);
}
// String Methods in JS :-
// toUpperCase() - Converts a string to uppercase letters
console.log(name.toUpperCase()); // Output: RITIK
// toLowerCase() - Converts a string to lowercase letters
console.log(name.toLowerCase()); // Output: ritik
// charAt() - Returns the character at the specified index
console.log(name.charAt(0)); // Output: R
// indexOf() - Returns the index of the first occurrence of a specified value in a string
console.log(name.indexOf("i")); // Output: 1
// lastIndexOf() - Returns the index of the last occurrence of a specified value in a string
console.log(name.lastIndexOf("i")); // Output: 4
// includes() - Checks if a string contains a specified value
console.log(name.includes("i")); // Output: true
// startsWith() - Checks if a string starts with a specified value
console.log(name.startsWith("R")); // Output: true
// endsWith() - Checks if a string ends with a specified value
console.log(name.endsWith("k")); // Output: true
// Trim() - Removes whitespace from both ends of a string
let nameWithSpaces = " Ritik Rana ";
console.log(nameWithSpaces.trim()); // Output: "Ritik Rana"
// String concatenation
let firstName = "Ritik";
let lastName = "Rana";
let fullName = firstName.concat(" ", lastName);
console.log(fullName); // Output: Ritik Rana
// String slicing
console.log(name.slice(0, 3)); // Output: Rit
// String replacing
console.log(name.replace("i", "u")); // Output: Rutik
// String repeating
console.log("Ritik".repeat(3)); // Output: RitikRitikRitik
// String splitting
let nameArray = name.split("");
console.log(nameArray); // Output: ["R", "i", "t", "i", "k"]
// String joining
nameArray = ["R", "i", "t", "i", "k"];
console.log(nameArray.join("")); // Output: Ritik