Skip to content

Commit 0129b29

Browse files
authored
Strings in JavaScript
1 parent c503bea commit 0129b29

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

strings.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Strings in JavaScript
2+
3+
let str1 = "Pranav";
4+
let str2 = "Tripathi";
5+
// To get the character at the desired position
6+
console.log(str1.charAt(2));
7+
8+
// Concatenating two strings
9+
// There are two ways to concatenate strings in JS
10+
console.log(str1 + str2);
11+
let concatenatedString = str1.concat(str2);
12+
console.log(concatenatedString);
13+
14+
// To search for the index of any character in the string
15+
console.log(str1.indexOf("a", 0));
16+
17+
console.log(str2.lastIndexOf("h", str2.length));
18+
19+
// Slicing a string
20+
let newString = str1.slice(0, 3);
21+
console.log(newString);
22+
23+
// To get the length of the string
24+
console.log(str1.length);
25+
console.log(str2.length);
26+
27+
// Spliting a string
28+
let splitString = str2.split("t", 4);
29+
console.log(splitString);
30+
31+
// Returning a substring
32+
console.log(str2.substring(3, 7));
33+
34+
// Converting the cases
35+
let upString = str1.toUpperCase();
36+
console.log(upString);
37+
let lowerString = str2.toLowerCase();
38+
console.log(lowerString);
39+
40+
// Searching for substring
41+
console.log(str2.search("path"));
42+
43+
// Slicing string using SUBSTR method
44+
let slicedString = str1.substr(0, 3);
45+
console.log(slicedString);

0 commit comments

Comments
 (0)