Strings and Text
Split and Join
Break text into parts and join it again.
split-join
`split` turns one string into an array. `join` turns the array back into a string with a chosen separator.
Split and Join
split_join.js
Replay: real traced execution (multi-file project)
const glue = "to";
const text = "red green blue";
const parts = text.split(" ");
const label = parts.join(glue);
console.log("count=" + parts.length);
console.log("label=" + label);
console.log("last=" + parts[parts.length - 1]);
const glue = "plus";
const text = "red green blue";
const parts = text.split(" ");
const label = parts.join(glue);
console.log("count=" + parts.length);
console.log("label=" + label);
console.log("last=" + parts[parts.length - 1]);
const glue = "and";
const text = "red green blue";
const parts = text.split(" ");
const label = parts.join(glue);
console.log("count=" + parts.length);
console.log("label=" + label);
console.log("last=" + parts[parts.length - 1]);
glue ← to, text ← red green blue, parts ← red,green,blue, label ← redtogreentoblue
1const glue→ to = "to"; //@glue="plus", "and"2const text→ red green blue = "red green blue";3const parts→ red,green,blue = textred green blue.split(" ");4const label→ redtogreentoblue = partsred,green,blue.join(glueto);56console.log("count=" + parts.length3);7console.log("label=" + labelredtogreentoblue);8console.log("last=" + parts[parts.length - 1]blue);outputcount=3 label=redtogreentoblue last=blue
glue ← plus, text ← red green blue, parts ← red,green,blue, label ← redplusgreenplusblue
1const glue→ plus = "plus";2const text→ red green blue = "red green blue";3const parts→ red,green,blue = textred green blue.split(" ");4const label→ redplusgreenplusblue = partsred,green,blue.join(glueplus);56console.log("count=" + parts.length3);7console.log("label=" + labelredplusgreenplusblue);8console.log("last=" + parts[parts.length - 1]blue);outputcount=3 label=redplusgreenplusblue last=blue
glue ← and, text ← red green blue, parts ← red,green,blue, label ← redandgreenandblue
1const glue→ and = "and";2const text→ red green blue = "red green blue";3const parts→ red,green,blue = textred green blue.split(" ");4const label→ redandgreenandblue = partsred,green,blue.join(glueand);56console.log("count=" + parts.length3);7console.log("label=" + labelredandgreenandblue);8console.log("last=" + parts[parts.length - 1]blue);outputcount=3 label=redandgreenandblue last=blue