String interpolation in Angular | String.format in typescript

In this tutorial, You will learn the string format function in typescript. In this example, how to implement string interpolation in typescript.

It uses to replace placeholders in a string template.

In javascript, there is no format function in a string object The same is not available natively in typescript

There are multiple ways to replace placeholder strings in typescript

ES6 template string strings

ES6 introduced template strings to replace placeholder values in a string.

let name = "john";
let str = "Hello {name} Welcome to My Blog";

Please note that template strings are enclosed in the backtick symbol not double(single) quotes.

Custom interface

interface  {
    format: (formatString: string, ...replacement: any[]) => string;
}
let str = 'Hello {name} Welcome to My Blog';

const finalStr = str.format(‘example’, ‘format’); // This is an example for format purpose

String interpolation in typescript

Typescript has support for interpolation string format, This has been available natively since typescript 1. x version.

interpolation string is enclosed in the backtick symbol

let name = "john";
let str = "Hello ${name} Welcome to My Blog";

Nodejs format

Nodejs has native util objects which have method format🔗

It is like printf in go language.

const util = require("util");
util.format("Hello %s ", "Franc");