-
Notifications
You must be signed in to change notification settings - Fork 12
/
section_b_13_string_functions.js
54 lines (28 loc) · 1.72 KB
/
section_b_13_string_functions.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
var str = 'The quick brown fox jumped over the lazy dog';
str.length(); // VM2349:1 Uncaught TypeError: str.length is not a function
str.length; // 44
str.charAt(2); // "e"
str.charAt(200); // ""
str.concat(' again and again'); // "The quick brown fox jumped over the lazy dog again and again"
str; // "The quick brown fox jumped over the lazy dog"
str.includes('quick'); // true
str.endsWith('dog'); // true
str.startsWith('Foo'); // false
str.repeat(5); // "The quick brown fox jumped over the lazy dogThe quick brown fox jumped over the lazy dogThe quick brown fox jumped over the lazy dogThe quick brown fox jumped over the lazy dogThe quick brown fox jumped over the lazy dog"
str.match(/((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}/) // null
'555-555-5555'.match(/((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}/) // (4) ["555-555-5555", "555-", undefined, "555-", index: 0, input: "555-555-5555"]0: "555-555-5555"1: "555-"2: undefined3: "555-"index: 0input: "555-555-5555"length: 4__proto__: Array(0)
str.replace('fox', 'wolf'); // "The quick brown wolf jumped over the lazy dog"
'555-555-5555 is my phone number'.search(/((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}/) // 0
'Hi, 555-555-5555 is my phone number'.search(/((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}/) // 4
str.indexOf('jumped'); // 20
str.lastIndexOf('jumped'); // 20
var str2 = str.concat('again and again');
str2.indexOf('again'); // 44
str2.lastIndexOf('again'); // 54
str.slice(4, 10); // "quick "
str.slice(-8); // "lazy dog"
str.slice(10); // "brown fox jumped over the lazy dog"
str.toUpperCase(); // "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG"
str.toLowerCase(); // "the quick brown fox jumped over the lazy dog"
var messyString = ' Hi there ';
messyString.trim(); // "Hi there"