JavaScript to cut a long string between words

Posted on 2011-10-20
Last Modified: 2012-05-12
Hi all,

I'm trying to figure out how to write a JavaScript function that will trim some text to a specific length, but not in the middle of a word - it could only trim on whitespace. So, if I called it like trimText(strTheText, 500), then it wouldn't necessarily trim at 500 - it might trim at 497 if there is a word between 498 and 506. We can assume that it always trims smaller than the given length. Even if there is a space at 501, which is closer to 500, it should still trim back at 493 or whatever.

This is what I started with, when I found it trimming in the middle of a word:

function trimText(strTheText, intSize) {
  if(strTheText.length <= intSize) {
    return strTheText;
  else {
    return strTheText.substr(0,500) + '...';

Anyone have any ideas on this? Code would be best ... thanks!

Question by:CAS-IT
    LVL 10

    Accepted Solution

    <script language="javascript">
    var strText = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
    //call trim text method
    var myTrimText = trimText(strText, 100);
    function trimText(str, len)
    	//Chop string at desired position.
    	var myString = str.slice(0, len);
    	//retrieve length of string up to but not including the last space.
    	var result = myString.substr(0, myString.lastIndexOf(" "));
    	//return the result.
    	return result;
    	return str.substr(0, str.slice(0, len).lastIndexOf(" "));

    Open in new window


    Author Comment

    Wow, 1 line!!

    Shame I get an error.

    TypeError: str.slice is not a function

    Well, I ran through it, and using it the long way (without everything on 1 line) works.

    Here's my final:

    $("#myDiv .stuff").each(function(){
        var theString = String($(this).text());
        theString = theString.slice(0, 75);
        theString = theString.substr(0, theString.lastIndexOf(" "));
        $(this).text(theString + " ...");

    LVL 10

    Expert Comment

    He CAS-IT, Thanks!

    Im sorry about error. I will have take another look. I thought i had it right. Good to see you have it working. Good job converting to jquery!


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now