Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question how to trim a long word

    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,

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by sidesmora View Post
    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

    Code:
    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.

    Code:
    a = new String("dooooooooooooooooooooooooooooooo");
    alert(a.substring(0,2))
    Last edited by DaveyErwin; 09-03-2010 at 11:09 AM.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,159
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    Code:
    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:-

    Code:
    <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
    Last edited by Philip M; 09-03-2010 at 11:54 AM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •