doforumda
doforumda

Reputation: 174

Manipulating a string in javascript

If I have a long string, lets say

var str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eu accumsan urna. Phasellus imperdiet elementum massa sit amet condimentum. Vivamus porttitor lobortis dignissim. Nam non tellus sapien, at pulvinar augue. Nulla metus mauris, cursus a sodales varius, imperdiet nec nisi. Quisque ut est quis massa sagittis pharetra quis aliquam dui. Phasellus id nisi quis mauris ultricies tristique et ut orci.";

and I want to manipulate it to display it something like this

"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eu..."

How can I do that?

Upvotes: 0

Views: 907

Answers (7)

yoozer8
yoozer8

Reputation: 7489

You can do this with simple javascript string manipulation.

var limit = 30 //or wherever you want to cut it off
str = str.substring(0, Math.Min(limit, str.length)) + '...'

EDIT:
You do not need the Math.Min as javascript's substring method will take care of the issue if the limit is longer than the string.

Note: This will append "..." even if your string is too short and is displayed in its entirety. See hunter's answer for how to avoid this.

Upvotes: 5

Richard JP Le Guen
Richard JP Le Guen

Reputation: 28753

You could use a regular expression replace:

str = str.replace(/^(.{0,29}[\w\d])(\b.+)?$/, function($0, $1) {
   if($1.length < str.length) {
       return $1+'...';
   }
   return $0;
});

This will:

  • If str is shorter than 30 characters, the result is the entire string.
  • It str is longer than 30 characters, it will be cut off at a word boundary and a ... will be appended to it. This ensures you don't end up with results like Lorem ipsum dolor sit amet,... (with the comma before the ...)

To change it to truncate at 66 characters...

str = str.replace(/^(.{0,65}[\w\d])(\b.+)?$/, function($0, $1) {
   if($1.length < str.length) {
       return $1+'...';
   }
   return $0;
});

Upvotes: 0

Mohsen
Mohsen

Reputation: 65785

You need to learn JavaScript then use jQuery! All the string manipulation can be done with JavaScript. You should use functions like .substring() .substr() .slice() and so...

In this case you can use substr:

str.substr(0, str.indexOf('eu') + 2) + '...';

Upvotes: 0

Phil
Phil

Reputation: 2307

var shortStr = str.substr(0, 66) + '...';

Upvotes: 0

Quentin
Quentin

Reputation: 943569

jQuery, while providing many useful bits of prewritten code, isn't aimed at people doing string manipulation.

Just use substring

var truncated = str.substring(0, 40);
if (truncated !== str) {
    str = truncated + "…";
}

Upvotes: 4

genesis
genesis

Reputation: 50976

You can use substr

var limit = 100;
str = str.substring(0,limit) + '...';

Upvotes: 0

hunter
hunter

Reputation: 63522

Try using the substring() method

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substring

Returns a subset of a string between one index and another, or through the end of the string.

var max = 20;

str = str.length > max 
    ? str.substring(0, max - 3) + "..." 
    : str;

Upvotes: 6

Related Questions