TypeScript String.raw() Method
TypeScripts String.raw() method is used for obtaining the string representation of template literals without interpreting escape characters.
Syntax
String.raw(string: TemplateStringsArray, ...values: any[]): string
String.raw`templateString`;
Parameters
- string (TemplateStringsArray): Here you have to pass the template literal array object.
- values (any[]): Here you have to pass the value containing the substituted values.
- templateString: Here you have to pass the template literal string.
Return Value
It will return the raw string form of the template literal.
Example 1: To demonstrate preserving the escape sequences.
Javascript
const filePath: string = String .raw`C:\Users\username\Documents\myfile.txt`; console.log(filePath); |
Output:
C:\Users\username\Documents\myfile.txt
Example 2: To demonstrate using raw() method with unicode characters.
Javascript
const unicodeRegular: string = `\u2607`; const unicodeRaw: string = String.raw`\u2607`; console.log( "Unicode Regular:" ); console.log(unicodeRegular); console.log( "\nUnicode Raw:" ); console.log(unicodeRaw); |
Output:
Unicode Regular:
☇
Unicode Raw:
\u2607