truncate

Modified on Wed, 02 Aug 2023

Description

Truncate string if it's longer than the given maximum string length. The last characters of the truncated string are replaced with the omission string which defaults to '...'

Syntax

truncate(text, options)

Parameters

Input ParameterTypeDescription
textstringThe string to truncate.
options (optional)objectThe options object. It can be used to configure length (the maximum string length), omission (the string to indicate text is omitted) and the separator (the separator pattern to truncate to) settings. EX: {'length' : 30, 'omission' = '...', separator = '--'}

Return value

Type
string

Sample

ExampleResultDescription
truncate('abcd1234abcd1234abcd1234abcd1234')'abcd1234abcd1234abcd1234abc...'Truncate string with default settings : maximum length 30, omission string '...'
truncate('abcd1234abcd1234', {'length' : 10, 'omission' : '**'})'abcd1234**'Truncate string to maxiumum length 10 with omission string as '**'
truncate('abcd1234abcd1234', {'length' : 10, 'omission' : '**', 'separator' : '1'})'abcd**'Truncate string to maxiumum length 10 with omission string as '**' and with seperator '1'

To view all the String functions, click here.