Skip to content

Commit ca3b4ab

Browse files
authored
Add files via upload
1 parent b7fa5ed commit ca3b4ab

File tree

1 file changed

+215
-0
lines changed

1 file changed

+215
-0
lines changed

Ch 4 String.js

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
// String
2+
3+
// A string is a sequence of characters enclosed in single quotes (') or double quotes ("). Strings are immutable, meaning that once created, they cannot be changed. However, string methods can be used to manipulate and extract information from strings.
4+
5+
// String are Used To Store And Manipulate Text
6+
7+
// String -> Collection Of Characters
8+
9+
// Single Quotes -> let name = 'dp';
10+
// Double Quotes -> let name = "dp";
11+
12+
let name = "dp";
13+
// console.log(name[3]); -> undefined
14+
15+
16+
// Template Literals
17+
18+
// After ES6 -> Template Literals Came In Use -> Use Back Tic
19+
20+
let boy1 = "hmm"
21+
let boy2 = 'ok'
22+
23+
// Print Hmm nice ok
24+
// let sentence = `boy1 "nice" 'is' boy2` -> We a Make String -> Use Single Or Double Quotes Both Usage Possible If String Made Through Back Tic
25+
26+
// String Interpolation
27+
let sentence = `${boy1} nice ${boy2}`
28+
console.log(sentence)
29+
// hmm nice ok
30+
31+
let fruit = `Bana\'na` -> Bana'na
32+
console.log(fruit)
33+
34+
// \' -> Escape Character -> Count As One Character
35+
// \n -> New Line
36+
// \t -> Tab
37+
// \r -> Carrige Return
38+
39+
40+
// String Method & Properties
41+
// Accessing Characters:
42+
// Individual characters in a string can be accessed using square brackets and their index. Remember, indexing starts at 0.
43+
44+
// const message = 'Hello';
45+
// console.log(message[0]); // Output: H
46+
// console.log(message[3]); // Output: l
47+
48+
// String Concatenation:
49+
// Strings can be concatenated using the + operator or the concat() method.
50+
51+
52+
// const firstName = 'John';
53+
// const lastName = 'Doe';
54+
// const fullName = firstName + ' ' + lastName;
55+
// console.log(fullName); // Output: John Doe
56+
57+
// // Using concat()
58+
// const message = 'Hello, ';
59+
// const name = 'John';
60+
// const greeting = message.concat(name);
61+
// console.log(greeting); // Output: Hello, John
62+
63+
// Finding Substrings:
64+
// The indexOf() method returns the index of the first occurrence of a substring within a string. It returns -1 if the substring is not found.
65+
66+
// const message = 'Hello, world!';
67+
// console.log(message.indexOf('world')); // Output: 7
68+
// console.log(message.indexOf('open')); // Output: -1
69+
70+
71+
// Extracting Substrings:
72+
// The slice() method extracts a portion of a string based on the start and end indexes.
73+
74+
// const message = 'Hello, world!';
75+
// console.log(message.slice(0, 5)); // Output: Hello
76+
// console.log(message.slice(7)); // Output: world!
77+
78+
// Replacing Substrings:
79+
// The replace() method replaces a specified substring with another substring.
80+
81+
// const message = 'Hello, John!';
82+
// console.log(message.replace('John', 'Alice')); // Output: Hello, Alice!
83+
84+
// Splitting Strings:
85+
// The split() method splits a string into an array of substrings based on a specified delimiter.
86+
87+
// const message = 'Hello, World!';
88+
// console.log(message.split(' ')); // Output: ["Hello,", "World!"]
89+
90+
91+
// Checking if a String Contains a Substring:
92+
// The includes() method checks if a string contains a specified substring and returns true or false.
93+
94+
// const message = 'Hello, World!';
95+
// console.log(message.includes('World')); // Output: true
96+
// console.log(message.includes('open')); // Output: false
97+
98+
// String Length:
99+
// The length property returns the number of characters in a string.
100+
101+
// const message = 'Hello, world!';
102+
// console.log(message.length); // Output: 13
103+
104+
// String Slice:
105+
// The slice() method extracts a portion of a string based on the start and end indexes. It returns a new string.
106+
107+
// const message = 'Hello, world!';
108+
// console.log(message.slice(7)); // Output: world!
109+
// console.log(message.slice(0, 5)); // Output: Hello
110+
111+
112+
// String Substring:
113+
// The substring() method is similar to slice(), but it doesn't accept negative indexes. It returns a new string.
114+
115+
116+
// const message = 'Hello, world!';
117+
// console.log(message.substring(7)); // Output: world!
118+
// console.log(message.substring(0, 5)); // Output: Hello
119+
120+
121+
// String Substr:
122+
// The substr() method extracts a portion of a string based on the start index and length. It returns a new string.
123+
124+
125+
// const message = 'Hello, world!';
126+
// console.log(message.substr(7)); // Output: world!
127+
// console.log(message.substr(0, 5)); // Output: Hello
128+
129+
// String Replace:
130+
131+
// The replace() method replaces a specified substring with another substring. It returns a new string.
132+
133+
// const message = 'Hello, John!';
134+
// console.log(message.replace('John', 'Alice')); // Output: Hello, Alice!
135+
136+
// String ReplaceAll:
137+
// The replaceAll() method replaces all occurrences of a specified substring with another substring. It returns a new string. (Available from ECMAScript 2021)
138+
139+
140+
// const message = 'Hello, John! John is a good guy.';
141+
// console.log(message.replaceAll('John', 'Alice')); // Output: Hello, Alice! Alice is a good guy.
142+
143+
// String toUpperCase and toLowerCase:
144+
// The toUpperCase() and toLowerCase() methods convert a string to uppercase and lowercase, respectively. They return a new string.
145+
146+
147+
// const message = 'Hello, world!';
148+
// console.log(message.toUpperCase()); // Output: HELLO, WORLD!
149+
// console.log(message.toLowerCase()); // Output: hello, world!
150+
151+
152+
// String Concatenation:
153+
// The concat() method concatenates two or more strings and returns a new string.
154+
155+
156+
// const firstName = 'John';
157+
// const lastName = 'Doe';
158+
// console.log(firstName.concat(' ', lastName)); // Output: John Doe
159+
160+
161+
// String Trim:
162+
// The trim() method removes whitespace from both ends of a string and returns a new string.
163+
164+
// const message = ' Hello, world! ';
165+
// console.log(message.trim()); // Output: Hello, world!
166+
167+
168+
// String TrimStart and TrimEnd:
169+
// The trimStart() and trimEnd() methods remove whitespace from the beginning and end of a string, respectively. They return a new string. (Available from ECMAScript 2021)
170+
171+
// const message = ' Hello, world! ';
172+
// console.log(message.trimStart()); // Output: Hello, world!
173+
// console.log(message.trimEnd()); // Output: Hello, world!
174+
175+
// String PadStart and PadEnd:
176+
// The padStart() and padEnd() methods pad a string with a specified character to a given length. They return a new string. (Available from ECMAScript 2017)
177+
178+
// const message = 'Hello';
179+
// console.log(message.padStart(10, '*')); // Output: *****Hello
180+
// console.log(message.padEnd(10, '-')); // Output: Hello-----
181+
182+
183+
// String CharAt and CharCodeAt:
184+
// The charAt() method returns the character at a specified index in a string.
185+
// The charCodeAt() method returns the Unicode value of the character at a specified index in a string.
186+
187+
// const message = 'Hello';
188+
// console.log(message.charAt(0)); // Output: H
189+
// console.log(message.charCodeAt(0)); // Output: 72
190+
191+
192+
// String Split:
193+
// The split() method splits a string into an array of substrings based on a specified delimiter.
194+
195+
// const message = 'Hello, World!';
196+
// console.log(message.split(',')); // Output: ["Hello", " World!"]
197+
198+
// Method
199+
// String length
200+
// String slice()
201+
// String substring()
202+
// String substr()
203+
// String replace()
204+
// String replaceAll()
205+
// String toUpperCase()
206+
// String toLowerCase()
207+
// String concat()
208+
// String trim()
209+
// String trimStart()
210+
// String trimEnd()
211+
// String padStart()
212+
// String padEnd()
213+
// String charAt()
214+
// String charCodeAt()
215+
// String split()

0 commit comments

Comments
 (0)