-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_String.js
More file actions
153 lines (99 loc) · 5.89 KB
/
07_String.js
File metadata and controls
153 lines (99 loc) · 5.89 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
let name = "Hitarth";
let new_name = new String("New Hitarth");
console.log(typeof name) // string
console.log(typeof new_name) // object
// 1. Variable name has all the string methods (despite being type - string) like the variable new_name (which is type - string object) because of a process called Autoboxing.
// 2. Autoboxing is the process where JavaScript temporarily wraps a Primitive value in an Object so you can access Properties or Methods that belong to its corresponding object type.
// ------------------------------------------------------------------------------------------------
// STRING PROPERTIES & METHODS :- (All string methods return a new string. They don't modify the original string.)
// Common :
// .length - [property] - returns the length of a string
console.log(name.length); // 7
// .concat() - [method] - joins two or more strings
console.log(name + new_name); // HitarthNew Hitarth
console.log(name + " " + new_name); // Hitarth New Hitarth
console.log("Hello " + name); // Hello Hitarth
// .repeat() - [ method] - repeat the string with a specific number of copies
console.log(name.repeat(3)); // HitarthHitarthHitarth
// .split() - [method] - convert string to array
console.log(name.split()); // [ 'Hitarth' ]
console.log(name.split("")); // [ 'H', 'i', 't', 'a', 'r', 't', 'h' ]
console.log("The quick brown fox jumps over the lazy dog.".split(" ")); // [ 'The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.' ]
console.log(name.split("a")); // [ 'Hit', 'rth' ]
// .reverse() - [method] - reverse the string - string don't have .reverse() method, convert them in array first
console.log(name.split("").reverse().join("")); // htratiH
// .includes() - [method] - return a boolean value of the specified value if found in the string
console.log(name.includes("H")); // true
console.log(name.includes("P")); // false
// .search() - [method] - return the index of the specified value if found in the string
console.log(name.search("a")); // 3
console.log(name.search("z")); // -1 (if not found)
// ----------------------------------------------
// Converting String To Upper & Lower Case :
// .toUpperCase() - [method] - convert string to upper case
console.log(name.toUpperCase()); // HITARTH
// .toLowerCase() - [method] - convert string to lower case
console.log(name.toLowerCase()); // hitarth
// ----------------------------------------------
// String Starting & Ending :-
// .startsWith() - [method] - return a boolean value of the specified value is found at the beginning of the string
console.log(name.startsWith("H")); // true
console.log(name.startsWith("h")); // false
// .endsWith() - [method] - return a boolean value of the specified value is found at the end of the string
console.log(name.endsWith("h")); // true
console.log(name.endsWith("H")); // false
// ----------------------------------------------
// String Trim :
// .trim() - [method] - removes whitespace from both sides of a string
let white_space = " Hello World ";
console.log(white_space.trim()); // "Hello World"
// .trimStart() - [method] - removes whitespace from the beginning of a string
console.log(white_space.trimStart()); // "Hello World "
// .trimEnd() - [method] - removes whitespace from the end of a string
console.log(white_space.trimEnd()); // " Hello World"
// ----------------------------------------------
// String Indexing :
// .indexOf() - [method] - returns the index of a specified value in a string (only the first match)
console.log(name.indexOf("a")); // 3
console.log(name.indexOf("H")); // 0
console.log(name.indexOf("z")); // -1 (if not found)
// .lastIndexOf() - [method] - returns the index of a specified value in a string (only the last match)
console.log(name.lastIndexOf("a")); // 3
console.log("hitarth".lastIndexOf("h")); // 6
console.log(name.lastIndexOf("z")); // -1 (if not found)
// ----------------------------------------------
// String Padding :
// .padStart() - [method] - pads a string with another string (multiple times) until the resulting string reaches the given length
console.log(name.padStart(10, "*")); // ***Hitarth
// .padEnd() - [method] - pads a string with another string (multiple times) until the resulting string reaches the given length
console.log(name.padEnd(10, "*")); // Hitarth***
// - Both methods are for strings, to apply them on numbers first convert them in string (.toString()).
// ----------------------------------------------
// String Replace :
// .replace() - [method] - replace a specified value with another value in a string (only the first match)
console.log(name.replace("Hitarth", "New Hitarth")); // New Hitarth
// .replaceAll() - [method] - replace all occurrences of a specified value with another value in a string
console.log("Like YouTube!, Comment YouTube!, Subscribe YouTube!".replaceAll("YouTube", "PornHub")); // Like PornHub!, Comment PornHub!, Subscribe PornHub!
// ----------------------------------------------
// Extracting String Characters :
// .at() - [method] - allows negative indexes
console.log(name.at(3)); // a
// .charAt() - [method] - does not allow negative values
console.log(name.charAt(3)); // a
// .charCodeAt() - [method] - returns UTF-16 code units (an integer between 0 and 65535)
console.log(name.charCodeAt(3)); // 97
// .codePointAt() - [method] - returns full Unicde code units (an integer between 0 and 11,14,111)
console.log(name.codePointAt(3)); // 97
// Bracket Notation [] - [neither method, nor property] - treats string as array and returns value based on index
console.log(name[3]); // a
// ----------------------------------------------
// Extracting String Parts (end not included) :
// .slice() - [method]
console.log(name.slice(0, 3)); // Hit
console.log(name.slice(3)); // arth
// .substring() - [method]
console.log(name.substring(0, 4)); // Hita
console.log(name.substring(4)); // rth
// .substr() - [method] - deprecated
console.log(name.substr(0, 5)); // Hitar
console.log(name.substr(5)); // th