-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.js
78 lines (69 loc) · 3.44 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
66
67
68
69
70
71
72
73
74
75
76
77
78
//DECLARE VARIABLE STRINGS
var firstName = "Khaled";
var lastname = "Naleem";
//________________________________________________________________________________________________________________________
//ESCAPING LITERAL QUOTES IN STRINGS
var myStr = "I am a \"double quoted\" string inside \"double quotes\"";
console.log(myStr)
//___________________________________________________________________________________________________________________________
//___________________________________________________________________________
/*** CHARACTERS
* \' = single quote
* \" = double quote
* \\ = backlash
* \n = newline
* \r = carriage return
* \t = tab
* \b= backspace
* \f = form feed
*/
var myStr2 = "FirstLine\n\t\\SecondLine\nThirdLine";
console.log(myStr2)
//_____________________________________________________________________________________________________________________
//CONCATENATE STRINGS WITH THE PLUS OPERATOR
var ourStr = "I come first. " + "I come second.";
console.log(ourStr)
//_____________________________________________________________________________________________________________________
//CONCATENATE STRINGS WITH THE PLUS EQUALS OPERATOR
var ourStr2 = "I come first. ";
ourStr2 += "I come second.";
console.log(ourStr2)
//_________________________________________________________________________________________________________________
//CONSTRUCTING STRINGS WITH VARIABLES
var myName = "Khaled";
var myStr3 = "Hello, my name is " + myName + ", how are you?";
console.log(myStr3)
//___________________________________________________________________________________________________________________
//APPENDING VARIABLES TO STRINGS
var anAdjective = "awesome!";
var ourStr3 = "I am ";
ourStr3 += anAdjective;
console.log(ourStr3)
//____________________________________________________________________________________________________________________
//FIND LENGTH OF THE STRING
var firstNameLength = 0;
var firstName1 = "Khaled";
firstNameLength = firstName1.length;
console.log(firstNameLength)
//________________________________________________________________________________________________________________
//BRACKET NOTATION TO FIND FIRST CHARACTER IN STRING
var firstLetterofFirstName = "";
var FirstName2 = "Lee";
firstLetterofFirstName = FirstName2[0];
console.log(firstLetterofFirstName)
//_________________________________________________________________________________________________________________
//STRING IMMUTABILITY
var myStr4 = "jello World";
//myStr4[0] = "h"; //THIS LINE OF CODE WILL RETURN AN ERROR SINCE YOU CAN'T CHANGE AN INDIVIDUAL CHARACTER IN A STRRING
myStr4 = "hello world!"; //YOU CAN CHANGE THE WHOLE VARIABLE AND NOT INDIVIDUAL CHARACTERS FROM THE VARIABLE
//_______________________________________________________________________________________________________________________
//BRACKET NOTATION TO FIND THE LAST CHARACTER IN A STRING
var lastName1 = "William";
var lastLetterOfLastName = lastName1[lastName1.length - 1];
console.log(lastLetterOfLastName)
//______________________________________________________________________________________________________________________________
//BRACKET NOTATION TO FIND Nth-TO-LAST CHARACTER IN A STRING
var lastname2 = "Marcus";
var thirdToLastLetterofLastName = lastname2[lastname2.length - 3];
console.log(thirdToLastLetterofLastName)
//______________________________________________________________________________________________________________________________________