Skip to content

Latest commit

 

History

History
112 lines (80 loc) · 3.13 KB

File metadata and controls

112 lines (80 loc) · 3.13 KB

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.

  • From start index, up to but excluding the end index.
  • If the first index is greater than the second, they are swapped.
  • If a second parameter is ommitted, then it is start index to end of string
  • Any argument value that is less than 0 or greater than str.length is treated as if it were 0 and str.length, respectively.
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

  • From start index, up to but excluding the end index.
  • If the first index is greater than the second, empty string is returned.
  • If a second parameter is ommitted, then it is start index to end of string
  • Any argument value that greater than str.length is treated as str.length
  • Negative values are string.legth - value
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 ^