JavaScript Program to Truncate a String to a Certain Length and Add Ellipsis (...)
Last Updated :
28 May, 2024
In this article, we will see how to truncate a string to a certain length and add an ellipsis (...) in JavaScript. To truncate a string to a specific length and add an ellipsis (...) in JavaScript means shortening the string to a predefined character count, indicating that the content continues beyond the truncated portion.
There are several methods that can be used to truncate a string to a certain length and add ellipsis (...) in JavaScript, which are listed below:
We will explore all the above methods along with their basic implementation with the help of examples.
Approach 1: Using String Manipulation
In this approach, we are using String.slice() method To truncate a string and add ellipsis using String.slice(), check if the string length exceeds a limit, then use slice to extract characters and append '...' for truncation.
Syntax:
const combinedString = str1 + " " + str2;
Example: In this example, The truncateString function shortens inputString to 20 characters, adding '...' if it exceeds the limit.
JavaScript
function truncateString(str, maxLength) {
if (str.length > maxLength) {
return str.slice(0, maxLength - 3) + '...';
}
return str;
}
let inputString = 'This is a Geeks for geeks article';
let truncatedString = truncateString(inputString, 20);
console.log(truncatedString);
OutputThis is a Geeks f...
In this approach we are using substring() method to truncate a string. If the input string surpasses the specified limit, it extracts the first characters and appends '...' to indicate truncation.
Syntax:
string.substring(startIndex, endIndex);
Example: In this example, function truncateString shortens inputString to 25 characters, adding '...' if it exceeds the limit.
JavaScript
function truncateString(str, maxLength) {
if (str.length > maxLength) {
return str.substring(0, maxLength - 3) + '...';
}
return str;
}
let inputString = 'This is a Geeks for geeks article';
let truncatedString = truncateString(inputString, 25);
console.log(truncatedString);
OutputThis is a Geeks for ge...
In this approach, template literal and conditional (ternary) operator. If the length exceeds a limit, truncate and add '...'; otherwise, return the original string.
Syntax:
`string text ${expression} string text`
Example: In this example we are using the above-explained approach.
JavaScript
function truncateString(str, maxLength) {
return str.length > maxLength ?
`${str.slice(0, maxLength - 3)}...` : str;
}
let inputString = 'This is a Geeks for geeks article';
let truncatedString = truncateString(inputString, 30);
console.log(truncatedString);
OutputThis is a Geeks for geeks a...
Approach 4: Using Array.prototype.join()
In this approach, we will use the Array.prototype.join() method to concatenate the truncated string and the ellipsis. This method involves splitting the string into an array of characters up to the specified length, joining them back into a string, and then appending the ellipsis if necessary.
Example: In this example, the truncateString function shorten inputString to 15 characters, adding '…' if it exceeds the limit.
JavaScript
function truncateString(str, maxLength) {
if (str.length > maxLength) {
return [str.slice(0, maxLength - 3), '...'].join('');
}
return str;
}
let inputString = 'This is a Geeks for geeks article';
let truncatedString = truncateString(inputString, 15);
console.log(truncatedString);
Similar Reads
JavaScript Program to Truncate a String to a Certain Length
In this article, we are going to learn how can we truncate a string to a certain length. Truncating a string to a certain length in JavaScript means shortening a string so that it doesn't exceed a specified maximum length. This can be useful for displaying text in user interfaces, such as titles or
3 min read
JavaScript Program for Generating a String of Specific Length
In this article, we are going to discuss how can we generate a string having a specific length. You might encounter many situations when working with JavaScript where you need to produce a string with a specified length. This could be done for a number of reasons, including generating passwords, uni
4 min read
JavaScript Program to Remove Last Character from the String
In this article, we will learn how to remove the last character from the string in JavaScript. The string is used to represent the sequence of characters. Now, we will remove the last character from this string. Example: Input : Geeks for geeks Output : Geeks for geek Input : Geeksforgeeks Output :
3 min read
JavaScript Program to Print all Subsequences of a String
A subsequence is a sequence that can be derived from another sequence by deleting zero or more elements without changing the order of the remaining elements. Subsequences of a string can be found with different methods here, we are using the Recursion method, Iteration method and Bit manipulation me
3 min read
JavaScript Program for Printing Shortest Common Supersequence
A Shortest Common Supersequence (SCS) is the shortest or smallest string that contains two given strings as a subsequence. It is a minimal combination of characters that includes all elements of both input strings. In this article, we will see different approaches for printing the shortest common su
8 min read
JavaScript URLify a given string (Replace spaces is %20)
In this article, we are going to discuss how can we URLify a given string using JavaScript. In which we will be transforming the spaces within the input strings into the "%20" sequence. The process of URLLification consists of replacing these spaces with the '%20' encoding. This task is essential fo
6 min read
JavaScript- Remove Last Characters from JS String
These are the following ways to remove first and last characters from a String:1. Using String slice() MethodThe slice() method returns a part of a string by specifying the start and end indices. To remove the last character, we slice the string from the start (index 0) to the second-to-last charact
2 min read
JavaScript - How to Pad a String to Get the Determined Length?
Here are different ways to pad a stirng to get the specified length in JavaScript.1. Using padStart() MethodThe padStart() method can be used to pad a string with the specified characters to the specified length. It takes two parameters, the target length, and the string to be replaced with. If a nu
3 min read
Java Program to Add Characters to a String
We will be discussing out how to add character to a string at particular position in a string in java. It can be interpreted as follows as depicted in the illustration what we are trying to do which is as follows: Illustration: Input: Input custom string = HelloOutput: --> String to be added 'Gee
4 min read
Java Program to Convert Long to String
The long to String conversion in Java generally comes in need when we have to display a long number in a GUI application because everything is displayed in string form. In this article, we will learn about Java Programs to convert long to String. Given a Long number, the task is to convert it into a
4 min read