The following are some of the most popular string functions. (Thank you ChatGPT for much of this)
.length
let str = "p5.js";
console.log(str.length); // Output: 5
.indexOf(searchValue)
Finds the first occurrence of a substring.
let str = "Hello, p5.js!";
console.log(str.indexOf("p5")); // Output: 7
.includes(searchValue)
Checks if a string contains a specific substring.
let str = "Hello, p5.js!";
console.log(str.includes("p5")); // Output: true
.startsWith(searchValue)
Checks if a string starts with a specific substring.
let str = "p5.js is fun!";
console.log(str.startsWith("p5")); // Output: true
.endsWith(searchValue)
Checks if a string ends with a specific substring.
let str = "Coding with p5.js";
console.log(str.endsWith("p5.js")); // Output: true
.toLowerCase() / .toUpperCase()
Converts a string to lowercase or uppercase.
let str = "p5.js";
console.log(str.toUpperCase()); // Output: P5.JS
.trim()
Removes whitespace from the beginning and end of a string.
let str = " p5.js ";
console.log(str.trim()); // Output: "p5.js"
.replace(searchValue, replaceValue)
Replaces occurrences of a substring.
let str = "Hello, p5.js!";
console.log(str.replace("p5.js", "world")); // Output: Hello, world!
.slice(start, end)
Extracts a part of a string.
let str = "p5.js is awesome!";
console.log(str.slice(0, 5)); // Output: p5.js
.substring(start, end)
Extracts a part of a string like .slice() but without negative indices.
let str = "p5.js is awesome!";
console.log(str.substring(0, 5)); // Output: p5.js