最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Manipulating a string in javascript - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Sep 22, 2011 at 16:17 yoozer8 7,4897 gold badges62 silver badges96 bronze badges asked Sep 22, 2011 at 16:12 doforumdadoforumda 1741 gold badge6 silver badges14 bronze badges 2
  • 4 You don't need jQuery for that. – Richard JP Le Guen Commented Sep 22, 2011 at 16:14
  • 2 You mean you want to remove the var str = from the beginning and the ; from the end? Can the variable have a different name than str? Please either describe the rules or list more examples. – Justin Morgan Commented Sep 22, 2011 at 16:14
Add a ment  | 

7 Answers 7

Reset to default 6

Try using the substring() method

https://developer.mozilla/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;

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.

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 + "…";
}

You can use substr

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

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) + '...';

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 ma 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;
});
发布评论

评论列表(0)

  1. 暂无评论