0

I have a problem I want to change the length of string. This is my string of characters that I want to change.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim .

I use substring function in javascript but the outputs is not like I need. this is my code

var str="Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ";
document.write(str.substring(100)+"<br />");

but the output is like this

laoreet dolore magna aliquam erat volutpat. Ut wisi enim 

My goal is just to make the length of that string of character to be 100 starting from the first character.

how i can solve this...thanks for your answer

3
  • 1
    how do you want to change it's length? by predetermined character length? by finding a certain word and clip it there? Commented Mar 13, 2012 at 5:07
  • does anyone use document.write anymore? The answer to your question is already in my answer. str.substring(0, 100). Commented Mar 13, 2012 at 5:20
  • document.write just to view my result. okay i fix this with your code.thanks you @mrtsherman. Commented Mar 13, 2012 at 5:25

1 Answer 1

6

It appears english is a second language for you and your question is very unclear. But based on what you posted I am guessing you want to truncate a string. This is straightforward using javascript's string methods.

http://www.quirksmode.org/js/strings.html

//example of truncating a string based on # of characters
var foo = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim."
var bar = foo.substring(0, 100);

//if you want to truncate at a particular word then use indexOf to find it in orig string
var banana = foo.substring(0, foo.indexOf('tincidunt'));
Sign up to request clarification or add additional context in comments.

1 Comment

Better more concise answer than mine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.