-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_methods.js
More file actions
47 lines (30 loc) · 1.18 KB
/
string_methods.js
File metadata and controls
47 lines (30 loc) · 1.18 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
// string methods = allow you to manipulate and work with text (strings)
let userName = " Rishaan ";
console.log(userName.charAt(0));
console.log(userName.indexOf("a"));
console.log(userName.lastIndexOf("a"));
console.log(userName.length);
console.log(userName.trim());
console.log(userName.toUpperCase());
console.log(userName.toLowerCase());
console.log(userName.repeat(5)); // no. of times you want to repeat
console.log(userName.startsWith("A")); //checks if it starts with mentioned or not
if(userName.startsWith("A")) {
console.log("Your username can't begin with 'A'");
}
else{
console.log(userName + "does not starts with 'A'!");
}
if(userName.includes(" ")) { // whitespace
console.log("Your username can't include ' '");
}
else{
console.log(userName + "does not starts with ' '!");
}
let phoneNumber = "123-456-7890";
phoneNumber = phoneNumber.replaceAll("-","");
console.log(phoneNumber);
phoneNumber = phoneNumber.padStart("15","A"); //15 is the size, A is the empty spaces filled to match the size
console.log(phoneNumber);
phoneNumber = phoneNumber.padEnd("20","B"); //15 is the size, A is the empty spaces filled to match the size
console.log(phoneNumber);