How to truncate text in Angular2? Last Updated : 31 Jan, 2020 Comments Improve Suggest changes Like Article Like Report Approach: It is easy to truncate text in Angular. The truncated text helps us to remove or cut-off portions of text. It abruptly ends the text, reducing the length of the text. It removes text in accordance with the truncate function used. Syntax: String length is greater than the given characters. function(str, length, ending) {........} String truncates to certain words length specified. function truncate(str, no_words) { return str.split(" ").splice(0, no_words).join(" "); } Example 1: This example shows the way text in Angular is made truncate. Here, the length of the text/string is longer than the given characters. The truncate function is created, which checks the condition if the string length is greater than the given length. It then returns the truncated text accordingly. html <!DOCTYPE html> <html> <head> <title>Truncate text in Angular2</title> <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js"> </script> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <script> text_truncate = function(str, length, ending) { if (ending == null) { ending = '...'; } if (str.length > length) { return str.substring(0, length - ending.length) + ending; } else { return str; } }; console.log(text_truncate('Truncate text using Geeks for Geeks',11)) console.log(text_truncate('Truncate text using Geeks for Geeks',11,'!!')) </script> </body> </html> Output: Example 2: This example shows how to truncate the text to certain words. It removes the words that are not included in the given limit. For this purpose: str.split, splice and join are used. html <!DOCTYPE html> <html> <head> <title>Angular JS Route Change</title> <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js"> </script> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <script> function truncate(str, no_words) { return str.split(" ").splice(0, no_words).join(" "); } console.log(truncate('Geeks for Geeks Portal: Truncate Text Using GFG', 6)); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to truncate text in Angular2? R riarawal99 Follow Improve Article Tags : AngularJS AngularJS-Misc Similar Reads How to truncate the text using Angular Pipe and Slice ? Angular Pipe and Slice can be used to manipulate or transform the data in a specific format. Angular Pipes can be utilized to format or transform data directly before displaying it in the Angular template. The Slice is a built-in method in JavaScript that enables the users to extract a part of the a 3 min read How to limit the length of a string in AngularJS ? Validation of fields is an important process while developing a dynamic web application. In various aspects of security, the validation is quite important. Along with this, the restriction of the limit to the length of a string is also an important process that helps to include the constraint for th 5 min read Text truncate in CSS CSS Text truncation allows you to control how much text overflows its container due to the distortion of the layout at the end of your design, making such text awkward. This is typically accomplished by adding an ellipsis (...) to the end of the text to denote that the text has been truncated. It is 5 min read How to truncate a file using Node.js ? In this article, we are going to explore How to truncate the complete file using Node. There are two methods to truncate the file of all its data. We can either use the fs.truncate() method or the fs.writeFile() method to replace all the file content. Method 1: The fs.truncate() method in Node.js ca 3 min read AngularJS lowercase Filter AngularJS provides different filters to format the data. The lowercase Filter formats the given string to the lowercase. In order to transmit & render the data from a TypeScript code to an HTML template (view), the interpolation concept can be utilized. The lowercase filter is piped with an expr 1 min read Like