-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20_string_methods.js
55 lines (34 loc) · 1.21 KB
/
20_string_methods.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
let a = 'Amrit'
// 1 LENGTH
console.log(a.length)
// 2 CASE CHANGE These DONOT do permanent changes to actual string
a.toUpperCase()
console.log(a)
console.log(a.toUpperCase())
a.toLowerCase()
console.log(a)
console.log(a.toLowerCase())
// 3 SLICE These DONOT do permanent changes to actual string
console.log(a.slice(2,4)) // DONOT includes 4
console.log(a.slice(2) ) // from index 2 to end
// 4 REPLACE These DONOT do permanent changes to actual string
let b = 'Amrit bhai bhai'
let c = b.replace('bhai','bhau') // changes first occurance only
let c2 = b.replaceAll('bhai','bhau') // changes all occurances
console.log(b)
console.log(c)
console.log(c2)
// 5 SPLIT
let f = b.split('') // make array by spltting string from all points of occurcance of ''
let g = b.split('i') // make array by spltting string from all points of occurcance of 'i'
console.log(f)
console.log(g)
// 6 CONCAT
let d = b.concat(' is ',a, ' bhai') // is same as let d = b+' is ' + a + 'bhai'
console.log(d)
// 7 TRIM
let e = ' hello my name is Amrit '
console.log(e)
console.log(e.trim())
// ----------> Strings are IMMUTABLE in Js
a[2] = 'B' // Donot changes anything