Skip to content

Latest commit

 

History

History
25 lines (20 loc) · 875 Bytes

indentString.md

File metadata and controls

25 lines (20 loc) · 875 Bytes
title tags
indentString
string,beginner

TS JS Deno

Indents each line in the provided string.

Use String.replace and a regular expression to add the character specified by indent count times at the start of each line. Omit the third parameter, indent, to use a default indentation character of ' '.

const indentString = (str: string, count: number, indent = " ") => {
  indent = indent.repeat(count);
  return str.replace(/^/gm, indent);
};
indentString("Lorem\nIpsum", 2); // '  Lorem\n  Ipsum'
indentString("Lorem\nIpsum", 2, "_"); // '__Lorem\n__Ipsum'