-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2. String.js
More file actions
63 lines (42 loc) · 1 KB
/
2. String.js
File metadata and controls
63 lines (42 loc) · 1 KB
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
// Strings are immutable (cannot change) all functions returns new string
s = " Asad "
// length
s.length
// compare
s === s2
// trim white spaces
s.trim()
// upper
s.toUpperCase()
// lower
s.toLowerCase()
// slice
s.slice(3,5)
// split(separator, [optional] limit)
// splits the string into substrings whenever a separator is found, and returns an array of those substrings
a = s.split(" ")
// capitalize
a.map(s=>{return (s.charAt(0)).toUpperCase()+s.slice(1)})
a.map(s=>{return (s.charAt(0)).toUpperCase()+s.substring(1)})
// concatenate
"hello" + "world"
// concatenate str to num
+"12" + +"15"
// template string using back ticks
`hi my name is ${s}`
// -------------- typecast
// number to string
console.log(typeof(22 + ""))
console.log(typeof(String(22)))
// string to number
console.log(typeof(+ "22"))
console.log(typeof(Number("22")))
// array to string
arr.join()
arr.toString()
// string to array
Array.from(s)
// searching in string
s.indexOf("hi")
s.startsWith("hello")
s.endsWith("world")