Gradient
Lesson 1.5Code~55 min

Text, sets & maps

Slice and split strings, count things with Maps, find unique things with Sets — the tools of every text model.

Module 1 · 0/8 done

Text is data too

Language models are trained on text, so we need to be good at handling it. A string is a sequence of characters. You can measure it, grab single characters by position (starting at 0, like arrays), cut slices out of it, change its case, search it, and split it into pieces.

Try itThe string toolbox
const s = "The Little Fox"

console.log(s.length)                 // 14 characters (spaces count)
console.log(s[0], s[s.length - 1])    // first and last character
console.log(s.toLowerCase())          // "the little fox"
console.log(s.slice(4, 10))           // characters 4 up to (not including) 10
console.log(s.includes("Fox"))        // true
console.log(s.indexOf("L"))           // where "L" first appears: 4
console.log(s.split(" "))             // ["The", "Little", "Fox"]
console.log(["a", "b", "c"].join("-"))  // "a-b-c"
console.log("  padded  ".trim())      // "padded"
console.log("banana".replaceAll("a", "o"))

for (const ch of "abc") console.log("letter:", ch)   // strings are loopable
Press Run (⌘/Ctrl + Enter) to see output here.