How To Capitalize The First Letter Of A String In Typescript

Capitalize the first letter of a String in TypeScript

Capitalize the first letter of a string in Typescript is a task which will be very helpful when you want to handle the user name. So how to do it? Let’s go into detail now.

Capitalize the first letter of a string in Typescript

Use the trim method

The trim() method will remove all white spaces of the string from the start and the end of the string.

Syntax:

string.trim()

Example:

const aString = '   Hello From Learn Share IT

// Remove all while space at first and end of the string
const trimedString = aString.trim()

console.log(trimedString)

Output:

[LOG]: "Hello From Learn Share IT"

Use the charAt() method

The charAt() method will help you return the letter at the specified index.

Syntax:

string.charAt(index)

Parameters:

  • index: Indicate the position in which you want to get the letter.

Example:

const aString = 'Hello From Learn Share IT'

// Return the letter at index 3
const aLetter = aString.charAt(3)

console.log(aLetter)

Output:

[LOG]: "l"

Use the toUpperCase() method

The toUpperCase() method will help you to capitalize all letters in the string.

Syntax:

string.toUpperCase()

Example:

const aString = 'Hello From Learn Share IT'

// Capitalize all letter in aString
const capitalizeStr = aString.toUpperCase()

console.log(capitalizeStr)

Output:

[LOG]: "HELLO FROM LEARN SHARE IT"

Use the slice() method

The slice method will help you return all characters you selected in the string.

Syntax:

string.slice(start, end)

Parameters:

  • start: The position that you want to start select the letter.
  • end: The position that you want to end select the letter.

Example:

const aString = 'Hello From Learn Share IT'

// Slice all letter from index 11 to index 25
const selectedLetter = aString.slice(11, 25)

console.log(selectedLetter)

Output:

[LOG]: "Learn Share IT"

If I don’t pass in the end parameters, it will default set to the length of the string.

Example:

const aString = 'Hello From Learn Share IT'

// Slice all letter from index 6 to the end
const selectedString = aString.slice(6)

console.log(selectedString)

Output:

[LOG]: "From Learn Share IT"

Capitalize the first letter of the string

The logic here is I will use the trim method to remove all white spaces and use charAt() method to get the letter at the first letter, then use the upperCase method to capitalize that letter, then use the slice method to concatenate with the last part of the string.

Example:

const aString = ' James '

// Remove while space
const trimedString = aString.trim()

// Get the first letter, capitalize it then concatenate with the left string
const changedString = trimedString.charAt(0).toUpperCase() + trimedString.slice(1)

console.log(changedString)

Output:

[LOG]: "James"

Summary

In this article, I showed you how to capitalize the first letter of a string in Typescript. You can combine all methods trim, charAt, toUpperCase, and slice methods to do it. I hope it helpful to you. Let’s try these methods!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *