How to trim a string to N characters in Javascript?

How can I, using Javascript, create a function that will trim the string passed as an argument to a given length, also passed as an argument. For example:

var string = "this is a string"; var length = 6; var trimmedString = trimFunction(length, string); // trimmedString should be: // "this is" 

Does anyone have any idea? I heard something about using a substring, but did not quite understand.

+126
javascript string trim
Sep 18 '11 at 18:53
source share
6 answers

Why not just use a substring ... string.substring(0, 7); The first argument (0) is the starting point. The second argument (7) is the end point (exception). More details here.

 var string = "this is a string"; var length = 7; var trimmedString = string.substring(0, length); 
+264
18 Sep '11 at 18:56
source share

Copying the comment into the answer because I found it useful:

 var string = "this is a string"; var length = 20; var trimmedString = string.length > length ? string.substring(0, length - 3) + "..." : string; 

Thank.

And jsfiddle for everyone who cares https://jsfiddle.net/t354gw7e/ :)

+37
Aug 27 '15 at 7:59
source share

I suggest using the extension for code accuracy. Note that extending the prototype of an internal object may potentially link to libraries that depend on them.

 String.prototype.trimEllip = function (length) { return this.length > length ? this.substring(0, length) + "..." : this; } 

And use it like:

 var stringObject= 'this is a verrrryyyyyyyyyyyyyyyyyyyyyyyyyyyyylllooooooooooooonggggggggggggsssssssssssssttttttttttrrrrrrrrriiiiiiiiiiinnnnnnnnnnnnggggggggg'; stringObject.trimEllip(25) 
+12
Mar 24 '17 at 18:43
source share
+7
Sep 18 '11 at 10:19
source share

A little late ... I had to answer. This is the easiest way.

 // JavaScript function fixedSize_JS(value, size) { return value.padEnd(size).substring(0, size); } // JavaScript (Alt) var fixedSize_JSAlt = function(value, size) { return value.padEnd(size).substring(0, size); } // Prototype (preferred) String.prototype.fixedSize = function(size) { return this.padEnd(size).substring(0, size); } // Overloaded Prototype function fixedSize(value, size) { return value.fixedSize(size); } // usage console.log('Old school JS -> "' + fixedSize_JS('test (30 characters)', 30) + '"'); console.log('Semi-Old school JS -> "' + fixedSize_JSAlt('test (10 characters)', 10) + '"'); console.log('Prototypes (Preferred) -> "' + 'test (25 characters)'.fixedSize(25) + '"'); console.log('Overloaded Prototype (Legacy support) -> "' + fixedSize('test (15 characters)', 15) + '"'); 

Step by step. .padEnd - Guaranteed string length

"The padEnd () method completes the current line with the given line (repeated if necessary) so that the resulting line reaches the specified length. The filling is applied from the end (right) of the current line. The source for this interactive Example is stored in the GitHub repository." Source: developer.mozilla .org / en-US / docs / Web / JavaScript / Reference / ...

.substring - restriction on the required length

If you decide to add ellipses, add them to the output.

I gave 4 examples of common use of JavaScript. I highly recommend using the String prototype with Overloading to support previous versions. This greatly facilitates implementation and change later.

+1
Jan 24 '19 at 0:35
source share
  let trimString = function (string, length) { return string.length > length ? string.substring(0, length) + '...' : string; }; 

Use case,

 let string = 'How to trim a string to N chars in Javascript'; trimString(string, 20); //How to trim a string... 
0
Jul 20 '19 at 16:02
source share



All Articles