String interpolation in Angular | String.format in typescript


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

This are used to replace placeholders in a string template.

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

There are multiple ways to replace placeholder string 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 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 a support for interpolation string format, This is avilable natively since typescript 1.x version.

interpolation string is enclosed in backtick symbol

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

Nodejs format

Nodejs has native util objects which has method format

It is like PrintF in golanguage

const util=require("util")
util.format("Hello %s ", "Franc")
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.