...

View Full Version : how to trim a long word



sidesmora
09-03-2010, 11:18 AM
hello,
i want to trim a word that too long..
example
"I don't know what i will dooooooooooooooooooooooooooooooo now"

how trim it with javascript function
so i will get
'I don't know what i will doooo... now"

any body help..

Regards,

DaveyErwin
09-03-2010, 12:01 PM
hello,
i want to trim a word that too long..
example
"I don't know what i will dooooooooooooooooooooooooooooooo now"

how trim it with javascript function
so i will get
'I don't know what i will doooo... now"

any body help..

Regards,

Because the string "dooooooooooooooooooooooooooooooo"
is an object you can call it's substring method like this


alert("dooooooooooooooooooooooooooooooo".substring(0,2))

However this code may be ill advised as I am beginning to
believe that this string is not an object and therefore
can not have methods.
So this alternative is much more advisable
because here we know for sure we are
handling an object.


a = new String("dooooooooooooooooooooooooooooooo");
alert(a.substring(0,2))

Philip M
09-03-2010, 12:45 PM
a = "dooooooooooooooooooooooooooooooo";
b = a.substring(0,5) + "...";
alert (b);


But obviously this will only work for the one word.

What exactly are you trying to do? Truncate all words in a textbox/textarea to maximum x characters? If so:-


<script type = "text/javascript">

var txt = "Loremipsum dolor sitamet, consectetur adipiscing elit. Etiam ipsumleo, scelerisque at dapibus ac, consectetur vel ipsum."
var txtArray = [];
txtArray = txt.split(" ");
for (var i = 0; i<txtArray.length; i++) {
if (txtArray[i].length > 5) {
txtArray[i] = txtArray[i].substring(0,5) + "...";
}
}
var newtxt = txtArray.join(" ");
alert (newtxt);

</script>


He thought he saw an Elephant,
That practised on a fife:
He looked again, and found it was
A letter from his wife.
"At length I realise," he said,
"The bitterness of Life!"
-Lewis Carroll



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum