-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_String.js
More file actions
81 lines (60 loc) · 2.77 KB
/
Copy path05_String.js
File metadata and controls
81 lines (60 loc) · 2.77 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
72
73
74
75
76
77
78
79
80
81
// Strings in JavaScript
// Strings in JavaScript are immutable, meaning they cannot be changed after they are created.
// Template literals (enclosed by backticks ``) are used for multi-line strings and string interpolation.
// String methods return new strings and do not modify the original string.
// Strings initialization
let singleQuoted = 'Hello, World!';
let doubleQuoted = "Hello, World!";
let multiLine = `This is a
multi-line string.`;
let myName = new String('hunain')
console.log(myName.__proto__);
// String concatenation
let greeting = "Hiee";
let name = "hunain";
let fullGreeting = greeting + ", " + name + "!";
console.log(fullGreeting);
// String interpolation
let formattedString = `OMG! ${greeting} ${name}, How are you?`;
console.log(formattedString);
// UpperCase and lowerCase
let upperCase = name.toUpperCase();
let lowerCase = name.toLowerCase();
let titleCase = name.charAt(0).toUpperCase() + name.slice(1).toLowerCase();
console.log(upperCase, lowerCase, titleCase);
// The 'includes()' method is used to check if a substring exists within another string.
let contain1 = fullGreeting.includes("Hiee");
let contain2 = fullGreeting.includes("hunain");
console.log(contain1, contain2);
// Trim whitespace
let whitespaceString = " Iam hunain mahudawala! ";
console.log(whitespaceString);
console.log(whitespaceString.trim());
// Replacing substrings
// If you need to replace all occurrences, use a regular expression with the global flag (e.g., /pattern/g).
let replacedString = fullGreeting.replace("Hiee", "How are you");
console.log(replacedString);
// Splitting and joining strings
// split() divides a string into an array based on a delimiter.
// join() is used to concatenate an array of strings into a single string with a specified separator.
let splitString = fullGreeting.split("Hiee");
let joinedString = splitString.join("Hiee");
console.log(splitString);
console.log(joinedString);
// Accessing characters
// The charAt() method returns the character at a specified index in a string.
let firstChar = greeting.charAt(0);
let lastChar = name.charAt(name.length - 1);
console.log(firstChar, lastChar);
// Extracting substrings
// substring() extracts characters from a string between two specified indices.
// slice() is similar but can also accept negative indices to count from the end of the string.
let subString = fullGreeting.substring(6, 13);
let sliceString = fullGreeting.slice(0, -1);
console.log(subString, sliceString);
// Finding index of a substring
// // indexOf() returns the index of the first occurrence of a specified value in a string.
// // lastIndexOf() returns the index of the last occurrence of a specified value in a string.
let indexOfhunain = name.indexOf("u");
let lastIndexOf = name.lastIndexOf("f");
console.log(indexOfhunain, lastIndexOf);