-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.js
More file actions
117 lines (83 loc) · 3.53 KB
/
String.js
File metadata and controls
117 lines (83 loc) · 3.53 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// 6 String Methods You Need To Know
// 1) .replace() and .replaceAll()
const stringWithThings = 'some-string-with-these-things';
// Replace a single occurrence of '-' with a space
const stringWithoutOneThing = stringWithThings.replace('-', ' ');
// Replace all occurrences of '-' with spaces
const stringWithoutAllThings = stringWithThings.replaceAll('-', ' ');
console.log(stringWithThings);
console.log(stringWithoutOneThing);
console.log(stringWithoutAllThings);
// 2) .slice()
const stringThatsTooLong = 'thisStringIsTooLong';
// Get a substring starting from index 5 to the end
const shorterString = stringThatsTooLong.slice(5);
// Get a substring from index 5 to 10 (exclusive)
const evenShorterString = stringThatsTooLong.slice(5, 10);
// Get the last character of the string
const lastCharacter = stringThatsTooLong.slice(-1);
// Get a substring from the third to the second-to-last character
const skipLastCharacter = stringThatsTooLong.slice(-3, -1);
console.log(shorterString);
console.log(evenShorterString);
console.log(lastCharacter);
console.log(skipLastCharacter);
// 3) .toLowerCase() and .toUpperCase()
const lowerCaseString = 'lowercasestring';
// Convert to uppercase
const upperCaseString = lowerCaseString.toUpperCase();
// Convert back to lowercase
const lowerCaseAgain = lowerCaseString.toLowerCase();
console.log(upperCaseString);
console.log(lowerCaseAgain);
// 4) .trim() and .trimStart() and .trimEnd()
const stringWithPadding = ' hello ';
// Remove leading and trailing spaces
const stringWithoutPadding = stringWithPadding.trim();
// Remove leading spaces
const stringWithoutStartPadding = stringWithPadding.trimStart();
// Remove trailing spaces
const stringWithoutEndPadding = stringWithPadding.trimEnd();
console.log({ stringWithPadding });
console.log({ stringWithoutPadding });
console.log({ stringWithoutStartPadding });
console.log({ stringWithoutEndPadding });
// 5) .indexOf() and .lastIndexOf()
const myName = 'Ruslan Tsykaliak';
// Find the index of the first occurrence of 'r'
const indexOfR = myName.indexOf('r');
// Find the index of 'r' starting from the second character
const indexOfRAfter4 = myName.indexOf('r', 1);
// Find the index of the substring 'li'
const indexOfPartialString = myName.indexOf('li');
// Find the index of the last occurrence of 'a'
const indexOfLastA = myName.lastIndexOf('a');
// Find the index of 'a' starting from the fifth character
const indexOfLastABefore4 = myName.lastIndexOf('a', 4);
console.log({ indexOfR });
console.log({ indexOfRAfter4 });
console.log({ indexOfPartialString });
console.log({ indexOfLastA });
console.log({ indexOfLastABefore4 });
// 6) .split()
const stringWithTexts = 'some-string-with-text';
// Split the string into an array of words using space as a delimiter
const arrayOfTexts = stringWithTexts.split(' ');
// Split the string into an array with a maximum length of 1
const arrayOfText = stringWithTexts.split('-', 1);
// Split the string into an array using '-with' as a delimiter
const arrayOfTextsWithMatchingMultiple = stringWithTexts.split('-with');
console.log({ stringWithTexts });
console.log({ arrayOfTexts });
console.log({ arrayOfText });
console.log({ arrayOfTextsWithMatchingMultiple });
// 1 Property - .length
const oneCharacter = 'R';
const bestProgrammist = 'Ruslan Tsykaliak';
// Get the length of the strings
const oneCharacterLength = oneCharacter.length;
const bestProgrammistLength = bestProgrammist.length;
console.log({ oneCharacter });
console.log({ bestProgrammist });
console.log({ oneCharacterLength });
console.log({ bestProgrammistLength });