A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/lodash-_-truncate-method/ below:

Lodash _.truncate() Method - GeeksforGeeks

Lodash _.truncate() Method

Last Updated : 17 Oct, 2024

The Lodash _.truncate() method of String is used to truncate the stated string if it is longer than the specified string length.

The last characters of the string which are truncated are replaced with the stated omission string which is by defaults "...".

Syntax:
_.truncate([string=''], [options={}])
Parameters:

This method accepts two parameters as mentioned above and described below:

Here, the options field are as follows:

Return Value:

This method returns the truncated string.

Example 1: This example shows the usage of the Lodash _.truncate method.

JavaScript
// Requiring lodash library
const _ = require('lodash');

// Calling _.truncate() method with 
// its parameter
let res = _.truncate(
  'GeeksforGeeks is a computer science portal.');

// Displays output
console.log(res);

Output:

GeeksforGeeks is a computer...

Here, the stated string is longer than the maximum length of the string so its truncated and the truncated string to be returned in the output must be of length 30 including the omission string.

Example 2: This example shows the implementation of the Lodash _.truncate() method.

JavaScript
// Requiring lodash library
const _ = require('lodash');

// Calling _.truncate() method with 
// its parameter
let res = _.truncate(
  'GeeksforGeeks, is a computer science portal.', {
     'length': 22,
     'omission': '***'
   }
);

// Displays output
console.log(res);

Output:

GeeksforGeeks, is a***

Here, the maximum length of the string as well as omission string both are specified. So, the resultant output is returned according to that.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4