Javatpoint Logo
Javatpoint Logo

How to truncate a string in JavaScript

To truncate a string in JavaScript, you can use the substring() method or the slice() method.

Example:

Here's an example using substring():

Output:

This is a 

In this example, substring() is used to extract the first 10 characters of the str string. The first argument of substring() is the starting index of the substring, and the second argument is the ending index (not inclusive). So, in this case, the substring will start at index 0 and end at index 10 (which includes the 10th character).

If you prefer to use slice(), you can do it like this:

Output:

This is a 

The slice() works in a similar way to substring(), with the first argument being the starting index and the second argument being the ending index (not inclusive).

Note that if the original string is less than the desired length, both methods will simply return the entire string. To add an ellipsis to indicate that the string has been truncated, you can do something like this:

Output:

This is a ...

In this example, maxLength is the maximum length of the truncated string, and the ternary operator checks if the length of the original string is greater than maxLength. If it is, then it uses substring() to extract the first maxLength characters and appends an ellipsis to the end of the truncated string. If the length of the string is less than or equal to maxLength, then it simply returns the original string.

Here's some additional code you can use to truncate a string in JavaScript:

Output:

This is a ...

In this code, a truncateString() function is defined that takes in two arguments: str, which is the string to be truncated, and maxLength, which is the maximum length of the truncated string. The function checks if the length of the str string is greater than maxLength. If it is, then it uses substring() to extract the first maxLength characters and appends an ellipsis to the end of the truncated string. If the length of the string is less than or equal to maxLength, then it simply returns the original string.

The truncateString() function can be called with different values of str and maxLength to truncate strings of varying lengths.

Here's another example using the slice() method:

Output:

This is a ...

This code is similar to the previous example, but uses the slice() method instead of substring() to extract the first maxLength characters of the string. The if statement checks if the length of the str string is greater than maxLength, and if so, the function uses slice() to extract the first maxLength characters and appends an ellipsis to the end of the truncated string. If the length of the string is less than or equal to maxLength, then it simply returns the original string.

Here's another example that uses a regular expression to truncate a string:

Output:

This is a ...






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA