Languages
[Edit]
EN

TypeScript - get first n characters of string

0 points
Created by:
Lillie-Rose-Finnegan
489

In this short article, we would like to show you how to get the first n characters of a string in TypeScript.

Example 1

Edit

The below example shows the use of .substring() method which returns the part of a string between the 0 and n indexes.

Runnable example:

Output:

Example 2

Edit

In the second example, we can see the result when we want to get a substring from a string in constious cases (like a string shorter than the number of characters we want to get or an empty string).

Runnable example:

Output:

Note:
Don't use substr() because it's a non-standard method.

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join