Post

3 followers Follow
0
Avatar

Smart way to shorten long strings with javascript

Hi people!

 

I need to cut a text field if it has more than 5 characters. If it cut, need to had ... at the end.

Ex:

1. This is a text. Result should be: "This is...

2. This is: Result should be: "This is"

 

Thanks a lot...

 

 

 

Tiago Lopes de Azevedo

Please sign in to leave a comment.

4 comments

0
Avatar

@textfield.substr(start position,number of characters)

i.e. @textfield.substr(0,5)

 

Andrew Wardle 0 votes
0
Avatar

Hi Tiago,

Note: "This is" are 7 characters not 5 (4 + 2 + blank space).

var text = @text field;
var len = text.length;
len > 7 ? text.substr(0,7) + "..." : text

Rainer

Rainer Grabowski 0 votes