-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrings.js
66 lines (35 loc) · 1.67 KB
/
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
// In JS textual data is stored as a string . there is no special type to store characters . The internal format is always UTF-16
// Strings can be enclosed within either single quotes, double quotes or backticks:
// Backticks allow us to add an expression
// Some special characters - \n new line , \\ backslash
// The length property has the strins length
let string = "sachin"
console.log(string.length) // 6
// To get a character at a position use square brackets
let string = "sachin"
console.log(string[1]) // a
console.log(string.charAt(1)) // a
// Iterating using for .. of
let string = "px12"
for(let char of string) {
console.log(char) // prints p x 1 2
}
// Strings are immutable . They can't be changed
// Methods toLowerCase() and toUpperCase() change the case
let string ="i am cool"
console.log(string.indexOf("am")) // 2
let string ="i am cool"
console.log(string.indexOf("am",2)) // 2
console.log(string.indexOf("am",2)) // 2
console.log(string.indexOf("am",3)) // -1
let string ="i am cool oo"
console.log(string.lastIndexOf("oo")) // 10
console.log(string.lastIndexOf("oos")) // -1
// includes, startsWith, endsWith are other string methods
// There are 3 methods in JavaScript to get a substring: substring, substr and slice.
// method selects… negatives
// slice(start, end) from start to end (not including end) allows negatives
// substring(start, end) between start and end negative values mean 0
// substr(start, length) from start get length characters allows negative start
// localeCompare can be used for string comparison
// Source https://javascript.info/string