-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_methods.js
More file actions
37 lines (34 loc) · 1.65 KB
/
string_methods.js
File metadata and controls
37 lines (34 loc) · 1.65 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
// A string in js is a sequence of characters, and it is immutable.
// but we can use it for some special purposes
/*
✅ Commonly Used String Methods:
Method Description
length Returns string length
toUpperCase() Converts to uppercase
toLowerCase() Converts to lowercase
charAt(index) Returns character at index
indexOf(substring) Returns index of first match (or -1)
lastIndexOf() Returns last index of match
includes(str) Checks if string contains str
startsWith(str) Checks if it starts with str
endsWith(str) Checks if it ends with str
replace(a, b) Replaces first a with b
replaceAll(a, b) Replaces all a with b (ES2021+)
trim() Removes whitespace
split(delimiter) Splits string into array
repeat(n) Repeats string n times
*/
let text = " Hello JavaScript! ";
console.log(text.length); // 21
console.log(text.trim()); // "Hello JavaScript!"
console.log(text.toUpperCase()); // " HELLO JAVASCRIPT! "
console.log(text.includes("Java")); // true
console.log(text.replace("Java", "Type")); // " Hello TypeScript! "
console.log(text.split(" ")); // ["", "", "Hello", "JavaScript!", "", ""]
console.log(text.charAt(0)); // " "
console.log(text.indexOf("Java")); // 7
console.log(text.lastIndexOf("!")); // 20
console.log(text.startsWith(" Hello")); // true
console.log(text.endsWith("! ")); // true
console.log(text.repeat(2)); // " Hello JavaScript! Hello JavaScript! "
console.log(text.split(" ").join("-")); // "--Hello-JavaScript!--"