-
Notifications
You must be signed in to change notification settings - Fork 0
/
4-Strings.js
165 lines (115 loc) · 8.32 KB
/
4-Strings.js
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
154
155
156
157
158
159
160
161
162
163
let name="Muqaddas Zahra"
console.log(name.length); //14 - including space, would have been 15 if tab used --tab=2spaces
//template string allow multiline strings
console.log(`My name is Muqaddas
This is JS Dive deep
Topic: Strings`)
//string interpolation
console.log(`My name is ${name}`)
//---------------------------------------------------------------------------------------------------------
console.log("...................");
let string1=new String("Zahra");
let string2= new String("Zahra");
console.log(string1 +"\n"+ string2)
console.log(string1==string2); //false, objects are compared by reference
let string="Zahra"
console.log(string==string1); //equal in value
console.log(string===string1); //not equal in type , string1 is of object type
//----------------------------------------------String Comparison---------------------------------------------
console.log(".........................");
console.log("zahra"<"Zahra"); //false ----> small letter > capital letter
console.log("zahra"=="Zahra"); //false
console.log("zahra">"Zahra"); //true
console.log("zara"<"zAra"); //false
console.log("zahra" > "zaara"); //true za same , h>a ---> compared letter alphabetically a<b
//----------------------------------------------String Methods---------------------------------------------------
//-----------------------------charAt &at --------------------------
// empty <---------charAt---------->empty
// reverse String<-----------at------------>undefined
console.log("..............................");
console.log("Muqaddas".charAt(3)); //a
console.log("Muqaddas".at(7)); //s
console.log("Muqaddas".charAt(8)); // (returns empty string)
console.log("Muqaddas".at(8)); // (returns undefined)
console.log("Muqaddas".charAt(-1)); // (returns empty string)
console.log("Muqaddas".at(-1)); //s --starts from end ---> last letter -1
console.log("Muqaddas".charAt(-3)); //empry string
console.log("Muqaddas".at(-3)); //d --starts from end ---> last letter -1
console.log("Muqaddas".at(-6)); //q --starts from end ---> last letter -1
console.log("Muqaddas".at(-9)); //undefined----when there is no letter available counting in reverse (starting from -1)
//string-->zahra
console.log(string.charAt(5)); //empty
console.log(string[5]); //undefined
console.log(string[4]); //a
string[0]="A"; //doesn't work--- property access is read only
console.log(string);
//-------------------------------------Extracting Parts of String (slice, substring, substr)--------------------------
// slice-------> -ve values reverse string
// substring---> -ve values treated as 0
// substr------> second arg is length. if length -ve, empty string
// slice % substring ---> ending index value is not extracted
let stringToSlice="MyCountryIsPakistan"
console.log(stringToSlice.slice(3,7)); //ount -- end is not included
console.log(stringToSlice.substring(3,7)); //ount -- end is not included
console.log(stringToSlice.substr(3,7)); //ountryI -->second param tells lenght of string
console.log(stringToSlice.slice(3)); // ountryIsPakistan---? starting from index 3, all remaining string
console.log(stringToSlice.substring(3)); // ountryIsPakistan---? starting from index 3, all remaining string
console.log(stringToSlice.substr(3)); // ountryIsPakistan---? starting from index 3, all remaining string
console.log(stringToSlice.slice(20)); // empty string
console.log(stringToSlice.substring(20)); // empty string
console.log(stringToSlice.substr(20)); // empty string
console.log(stringToSlice.slice(-3)); //tan---> starts from end with last letter starting at -1
console.log(stringToSlice.substr(-3)); //tan---> starts from end with last letter starting at -1
console.log(stringToSlice.substring(-3)); //MyCountryIsPakistan--> -ve value is treated as 0 in substring
console.log(stringToSlice.slice(-3,-6)); //empty string----can't print in reverse
console.log(stringToSlice.substring(-3,-6)); //empty string---- -ve value is treated as 0 in substring
console.log(stringToSlice.slice(-6, -3)); //kis --> starts from end with last letter starting at -1
console.log(stringToSlice.slice(-6, 3)); //empty string --> ending index 2 comes before starting index -6. starts from end with last letter starting at -1,
console.log(stringToSlice.slice(-6, 16)); //kis --> starts from end with last letter starting at -1 , ends at index 15(16 not included) starting from index 0
console.log(stringToSlice.substr(-6, -3)); //empty string , lenght -ve --> empty string
console.log(stringToSlice.substr(-6, 3)); //kis
console.log(stringToSlice.slice(-6,2)); //empty string -ending index 2 comes before starting index -6 , no letters exist in that range
console.log(stringToSlice.substring(-6,2)); //My ---> -ve values treated as 0 in substring
console.log(stringToSlice.slice(-6,0)); //empty string---
console.log("zahra".slice(-9,3)); //zah --minus values that are greater than the existing number of letters are treted as starting index 0
console.log("zahra".substr(-9,3)); //zah --minus values that are greater than the existing number of letters are treted as starting index 0
console.log("zahra".slice(-9,-1)); //zahr ---treated as (0,-1) --minus values that are greater than the existing number of letters are treted as starting index 0
//-------------------------------trim-- removes white spaces-------------------------
//removes whitespaces from sides of string
console.log("................");
let stringToTrim=" My Country Is Pakistan "
console.log(stringToTrim.length); //26
console.log(stringToTrim.trim().length); //22 -- removed two white spaces from each side of string
console.log(stringToTrim.trimStart().length); //24 -- removed two white spaces from start of string
console.log(stringToTrim.trimEnd().length); //24 -- removed two white spaces from end of string
//----------------------------padStart and padEnd-----------------------
//It pads a string with another string (multiple times) until it reaches a given length.
console.log(".........");
let stringToPad="Pakistan"
console.log(stringToPad.padStart(17, "P")); //PPPPPPPPPPakistan
console.log(stringToPad.padStart(17, "Pakistan")); //PakistanPPakistan
console.log(stringToPad.padStart(17, 92345)); //923459234Pakistan
console.log(stringToPad.padStart(17, true)); //truetruetPakistan-->true converts to string
console.log(stringToPad.padEnd(17, "P")); //PakistanPPPPPPPPP
console.log(stringToPad.padEnd(17, "Pakistan")); //PakistanPakistanP
console.log(stringToPad.padEnd(17, 92345)); //Pakistan923459234
console.log(stringToPad.padEnd(17, true)); //Pakistantruetruet
//---------------------------------------repeat------------------------------
//return number of copies of string----string.repeat(count)
console.log("Pakistan".repeat(3)); //PakistanPakistanPakistan
//------------------------------------replace and replaceAll---------------------------------
//replaces one value with other
let stringToReplace="Pakistan is Pakistan"
console.log(stringToReplace.replace("Pakistan","Kashmir")); //Kashmir is Pakistan ----only replaces the first match
console.log(stringToReplace.replace(/Pakistan/g,"Kashmir")); //Kashmir is Kashmir ----Re with flag /g to replace all matches
console.log(stringToReplace.replace("PAKISTAN","Kashmir")); //Pakistan is Pakistan ----case sensitive-- PAKISTAN didn't match with Pakistan
console.log(stringToReplace.replace(/PAKISTAN/i,"Kashmir")); //Kashmir is Pakistan -----Re with flag /i to avoid case sensitivity
console.log(stringToReplace.replace(/PAKISTAN/ig,"Kashmir")); //Kashmir is Kashmir -----Re with flag /i to avoid case sensitivity and g to replace all matches
console.log(stringToReplace.replace("Pakistan",9)); //9 is Pakistan
console.log(stringToReplace.replaceAll("Pakistan", "Kashmir")); //Kashmir is Kashmir
// console.log(stringToReplace.replaceAll("PAKISTAN/i", "Kashmir")); //error, replaceAll dont use flags directly, use replace with flags in this case
//--------------------------------------split---converts to array--------------------------------------------
console.log("Pakistan is my country".split(" ")); //['Pakistan', 'is', 'my', 'country'] (4)
console.log("Pakistan is , my country".split(",")); //['Pakistan is ', ' my country']] (2)
console.log("Pakistan is , my country".split()); //['Pakistan is , my country'] (1) param missing, stores whole string at index 0
console.log("Pakistan is , my country".split("")); //array of single characters