[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


JavaScript to cut a long string between words

Posted on 2011-10-20
Medium Priority
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
  • 2
LVL 10

Accepted Solution

Gregg earned 2000 total points
ID: 37001959
<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

ID: 37003780
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

ID: 37004228
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

873 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