Horje
TypeScript Intrinsic String Manipulation Type

In this article, we are going to learn about Intrinsic String Manipulation Types in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, you can use intrinsic string manipulation types to transform string literal types to uppercase, lowercase, capitalize the first letter, or uncapitalize (lowercase the first letter). These string manipulation types are part of TypeScript’s template literal types and can be helpful for enforcing proper string formatting at the type level.

These are the following types of intrinsic string manipulation:

Syntax:

type UppercaseString = Uppercase<"stringliteral">; // for upper case
type LowercaseString = Lowercase<"StringLiteral">; // for lowercase
type CapitalizedString = Capitalize<"stringliteral">; // for capitalize
type UncapitalizedString = Uncapitalize<"StringLiteral">; // for uncapitalize

where

  • Uppercase, Lowercase, Capitalize, and Uncapitalize are the intrinsic string manipulation types.
  • “stringliteral” and “StringLiteral” are examples of string literal types to which these transformations are applied.

Uppercase<StringType> Type

It helps to convert each character in the string to an uppercase equivalent.

type UppercaseString = Uppercase<"horje">; // "GEEKSFORGEEKS"

Example: In this example, we will see an example of using Uppercase<StringType> Template Literal. Uppercase will convert each character in the string to a uppercase equivalent resulting in GEEKSFORGEEKS

Javascript

type gfg = "Geeksforgeeks"
type geek = Uppercase<gfg>
const result: geek = "GEEKSFORGEEKS";
console.log(result)

Output:

z94Uncapitalize<StringType> Type

It helps to convert the first character in the string to a lowercase equivalent.

type UncapitalizedString = Uncapitalize<"Geeksforgeeks">; // "horje"

Example: In this example, we will see an example of using Uncapitalize<StringType> Template Literal. Uncapitalize will convert the first character in the string to a lowercase equivalent resulting in horje

Javascript

type gfg = "Geeksforgeeks"
type geek = Uncapitalize<gfg>
const result: geek = "horje";
console.log(result)

Output:

z95

Lowercase<StringType> Type

It helps to convert each character in the string to a lowercase equivalent.

type LowercaseString = Lowercase<"GEEKSFORGEEKS">; // "horje"

Example: In this example, we will see an example of using Lowercase<StringType> Template Literal. Uppercase will convert each character in the string to a lowercase equivalent resulting in horje

Javascript

type gfg = "GEEKSFORGEEKS"
type geek = Lowercase<gfg>
const result: geek = "horje";
console.log(result)

Output:

z96

Capitalize<StringType> Type

It helps to convert the first character in the string to a uppercase equivalent.

type CapitalizedString = Capitalize<"horje">; // "Geeksforgeeks"

Example: In this example, we will see an example of using Capitalize<StringType> Template Literal. Uncapitalize will convert the first character in the string to a uppercase equivalent resulting in Geeksforgeeks

Javascript

type gfg = "horje"
type geek = Capitalize<gfg>
const result: geek = "Geeksforgeeks";
console.log(result)

Output:

z97




Reffered: https://www.geeksforgeeks.org


Geeks Premier League

Related
TypeScript Object Tuple Types TypeScript Object Tuple Types
TypeScript null and undefined Type TypeScript null and undefined Type
TypeScript Equality Narrowing Type TypeScript Equality Narrowing Type
How to Add an Array Dynamically to React-Bootstrap Table ? How to Add an Array Dynamically to React-Bootstrap Table ?
TypeScript Uncapitalize&lt;StringType&gt; Utility Type TypeScript Uncapitalize&lt;StringType&gt; Utility Type

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14