View on GitHub

Matt's homepage

Bookmark this to keep an eye on new things I am learning.

Strings

Return Home

To upper and lower case

Converts a string to upper or lower case

let name = "Hananna";
console.log(name.toUpperCase());
console.log(name.toLowerCase());

^ back to top ^

IndexOf

Find the position in a string for another string

let name = "Hananna";
console.log(name.indexOf("anna"));

^ back to top ^

Substring

Extracts characters from position to position of a string.

const text = "0123456789";
console.log(text.substring(5, 7)); // 56 - start to end index
console.log(text.substring(5, 3)); // 34 - 1st greater than 2nd, so swapped
console.log(text.substring(5)); // 56789 - 2nd ommitted, so 1st to end
console.log(text.substring(-7,20)); // 0123456789 - 1st becomes 0, 2nd becomes 9

^ back to top ^

Slice

Extracts a section of this string and returns it as a new string, without modifying the original string

const text = "0123456789";
console.log(text.slice(5, 7)); // 56 - 5-6
console.log(text.slice(5, 3)); // "" - 1st greater than 2nd, empty string
console.log(text.slice(4)); // 456789 - 2nd ommitted, so 4 to end
console.log(text.slice(-4)); // 56789 - 2nd ommitted and -1st, so end-4 to end
console.log(text.slice(-4,8)); // 67 - End-4 to 7
console.log(text.slice(0,100)); // 67 - start to max string length (if 2nd higher)
console.log(text.slice(-4,-1)); // 67 - End-4 to End-1

^ back to top ^

Split

Splits a string by delimeter into an array

let phrase = "I like big butts, and I cannot lie";
let words = phrase.split(' ');
console.log(words); // retruns ['I', 'like', 'big', 'butts, ',', 'and', 'I', 'cannot', 'lie']

^ back to top ^

Replace

Searches and replaces something in a striing

let phrase = "I like big butts, and I cannot lie";
console.log(phrase.replace('butts', 'dinners'));  // returns "I like big dinners, and I cannot lie"

^ back to top ^

Trim

Removes leading and trailing whitespace’

phrase = "     This is a new phrase       ";
console.log(phrase);
console.log(phrase.trim());  // returns "This is a new phrase"

^ back to top ^