String methods
string.indexOf('') // gives position of needle in haystack
string.lastIndexOf('') // if there are multiple
sting.slice(i, ii) // remove the first i and last ii characters of string
string.slice(-i) // Save last i of string
string.slice(0, string.indexOf(' ')) // gets first word.
string.toLowerCase();
string.toUpperCase();
string.trim(); removes LEADING spaces and \n
const normalizedEmail = eMail.toLowerCase().trim();
// replacing
.replace(item, replacement) // replace first appearance
.replaceAll() // replaces all // ***New method***
.replace(/string/g, replacement) // replacing with global exoressions
// Booleans
string.includes()
string.startsWith()
string.endsWith()
//Split and join
console.log('a+lovely+string'.split(+)) // returns an array
const [firstName, lastName] = "Duncan Samwell".split(' ') // creates an array
const newName = ['Mr.' firstName, lastName].join();
//Capitalize function
const capitalizeName = function(name) {
const names = name.split(' ');
for (const n of names) {
n[0].toUpperClass() + n.slice(1);
n.push(n.replace(n[0], n[0].toUpperCase());
}
}
//Padding
string.padStart(n, p) // fill string with p until entire string is length of n
string.padEnd(n, p)
// Example for Credit Card
const maskCreditCard = function(cc) {
const l = cc.length;
const lastFour = cc.slice(-4);
return lastFour.padStart(l, '*');
}
//repeat
string.repeat(x) // repeat x amount of times
String Methods Practice
/*
///////////////////////////////////////
// String Methods Practice
*/
const flights =
'_Delayed_Departure;fao93766109;txl2133758440;11:25+_Arrival;bru0943384722;fao93766109;11:45+_Delayed_Arrival;hel7439299980;fao93766109;12:05+_Departure;fao93766109;lis2323639855;12:30';
// 🔴 Delayed Departure from FAO to TXL (11h25)
// Arrival from BRU to FAO (11h45)
// 🔴 Delayed Arrival from HEL to FAO (12h05)
// Departure from FAO to LIS (12h30)
const getCode = flights.split('+');
const l = getCode[0].length;
for (const line of getCode) {
const section = line.split(';');
// Status
let status = section[0].slice(1).replace('_', ' ');
if (status.startsWith('Delayed')) status = '🔴 ' + status;
// From
let from = section[1][0] + section[1][1] + section[1][2];
// To
let to = section[2][0] + section[2][1] + section[2][2];
// Clock
let clock = section[3].replace(':', 'h');
const output = status + ' from ' + from.toUpperCase() + ' to ' + to.toUpperCase() + ' (' + clock + ')';
console.log(output.padStart(l - 6));
}
Alternative
const flights =
'_Delayed_Departure;fao93766109;txl2133758440;11:25+_Arrival;bru0943384722;fao93766109;11:45+_Delayed_Arrival;hel7439299980;fao93766109;12:05+_Departure;fao93766109;lis2323639855;12:30';
// 🔴 Delayed Departure from FAO to TXL (11h25)
// Arrival from BRU to FAO (11h45)
// 🔴 Delayed Arrival from HEL to FAO (12h05)
// Departure from FAO to LIS (12h30)
const getCode = str => str.slice(0, 3).toUpperCase();
for (const flight of flights.split('+')) {
const [type, from, to, time] = flight.split(';');
const output = `${type.startsWith('_Delayed') ? '🔴' : ''}${type.replaceAll(
'_',
' '
)} ${getCode(from)} ${getCode(to)} (${time.replace(':', 'h')})`.padStart(36);
console.log(output);
}