Repository navigation
Expand file tree
/
Copy pathprogram5.js
More file actions
66 lines (49 loc) · 1.86 KB
/
Copy pathprogram5.js
File metadata and controls
66 lines (49 loc) · 1.86 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
64
65
66
// string related library functions
// length, prompt(), chartAt(5),
// string.toUpperCase(), string.toLowerCase()
// string1.concat(string2)
// string.slice(0,3)
// getting the input from user as string
var text = prompt("Enter your name : ");
document.write("Your name : " + text + "<br/>");
//finding the length of a string
var len = text.length;
document.write("Number of characters : " + len + "<br/>");
// finding the character at a specific position
document.write(text.charAt(2) + "<br/>");
// converting the string into uppercase or lowercase
document.write(text.toUpperCase() + "<br/>");
document.write(text.toLowerCase() + "<br/>");
// concatenation function
var text1 = " hi";
var text2 = "bye";
var text3 = text1.concat(text2);
document.write(text3 + "<br/>");
// slice function
var text4 = "hello";
var result = text4.slice(0, 2);
document.write(result + "<br/>");
//collected from w3school
var x = new String("John");
var y = new String("John");
// (x === y) is false because x and y are different objects
/*
There are 3 methods for extracting a part of a string:
slice(start, end)
substring(start, end)
substr(start, length)
1) slice can take positive and negative parameter.
If a parameter is negative, the position is counted from the end of the string.
var str = "Apple, Banana, Kiwi";
var res = str.slice(-12, -6); // output - Banana
var str = "Apple, Banana, Kiwi";
var res = str.slice(7); // output - Banana, Kiwi
2) substring() is similar to slice().
The difference is that substring() cannot accept negative indexes.
var str = "Apple, Banana, Kiwi";
var res = str.substring(7, 13); // output - Banana
3)substr() is similar to slice().
The difference is that the second parameter specifies the length of the extracted part.
var str = "Apple, Banana, Kiwi";
var res = str.substr(7, 6); // output - Banana
*/