Flash Vista - Home
Newest Cool Site
The Two Tales
Site info | Archive
Main Navigation
Home
New Links
Top Rated
Most Popular
Cool Sites
Search

Flash Tutorials
Flash Resources
Flash News
Flash Templates
Flash Intro Templates
Website Templates
Flash Games
Flash Books
FlashVista Polls
Sitemap


Random Link
English
Excite Media
Site info | Get another
FlashVista
Login
Register
Subscribe

Add Link
Modify Link
Favorites
Suggest Category

Advertise with us
Support us
Credits / Thanks
Contact

Flash Templates:

More Templates ...

Mailing List
User

Password



Register
Forgot password?
Partner websites
Flash Templates
Flash Components
Free Hit Counter
Free Seo Tools
Free Tutorials
Free Video Tutorials
Forum signatures
Other Resources
Suggest
Suggest this site to a friend


Mailing List
Status: Not logged in

English English German French Spanish Italian Portuguese Russian Polish Finnish Dutch Swedish Thai Romanian Traditional Chinese Simplified Chinese
SearchNot logged in
Keyword: Search for: Advanced Search


Reduce a string to a certain length


NEW Flash Tutorials in Video Format - Powered by LearnFlash.com: 45 minutes of flash tutorials now available in streaming format or download. Topics Include flash for beginners, text effects, actionscripting, audio/video, flash 8 and more.


Printer version



by Guy Watson
www.flashguru.co.uk

This morning i needed to reduce any given string to a certain length and then place a pattern at the end of the string, in this case a simple '....' but the reduction had to take into account full words, meaning, i didnt want to just split the string in any old place and leave parts of a word in the final string. So i created this little String Exstension which i thought may come in useful for some other people! Seems to work great for me....

You pass this method of the String object, a length to cut the string down to, and a pattern that you want to be returned at the end of the string:


this.onEnterFrame=function()
String.prototype.reduce=function(l,p)
{
if(this.length<=l) return this;
var words=this.split(" ")
var numWords=words.length
var output=[]
var ol,cWord,w
for(w=0;w {
cWord=words[w]
cwl=cWord.length
if((ol+cwl)<=l)
{
output.push(cWord)
ol+=cwl+1
}
else break
}
return output.join(" ")+(this.length>l) ? p
}


And here is how you would use this method:


rhyme="Bah bah, black sheep, have you any wool?"
cutRhyme=rhyme.reduce(22,"...")


For examples sake:


trace(cutRhyme);


Outputs:


this.onEnterFrame=function()
Bah bah black sheep...


(Added: 02-02-2004, Hits: 0, Rating: 3.10, Votes: 10, Reviews: 0)
Add to Favorites Suggest to a Friend

Reviews: (0)


Add Review
Please note:
We review EVERY comment before it appears on the site, so please dont waste your time by posting spam links :)
No URLs allowed, no HTML please.

If you register or login first, your review will contain your nickname


Rate It



Excellent!
Very Good
Good
Fair
Poor