www.webdeveloper.com
Results 1 to 7 of 7

Thread: Trim string length if above certain length

  1. #1
    Join Date
    Jul 2007
    Posts
    183

    Trim string length if above certain length

    Hi Guys

    I have a string. If it is longer than 25 chars I want to delete the excess chars. How can I do this?

  2. #2
    Join Date
    Jul 2007
    Posts
    183
    I have written some code but it doesnt work when I try to empty the charAt value.

    Code:
    var all = "1234567890123456789012345678901234567890";
    for(var k=0;k<all.length;k++) {
    	if(k>25) {
    		all.charAt(k) = ""; //This doesnt work
    	}
    }

  3. #3
    Join Date
    Aug 2007
    Posts
    3,767
    Code:
    str = str.substring(0,25);
    EDIT: Or in your style
    Code:
    var all = "1234567890123456789012345678901234567890";
    all = all.substring(0,25);
    As a general rule, try to keep things simple. JavaScript can work out whether the string is too long, you don't have to worry about it.
    Great wit and madness are near allied, and fine a line their bounds divide.

  4. #4
    Join Date
    Jul 2007
    Posts
    183
    Thanks. That was really simple in comparsion to what I was doing! I have another problem, at the end of some of my words I have a comma. How do I check for and delete it? Note that there will be commas in other parts of the sentence but the one I want to delete is the one that comes at the end of the word.

  5. #5
    Join Date
    Aug 2007
    Posts
    3,767
    I'm not sure exactly what you mean. "hello, hello, hello," should one or three commas be deleted. I'll presume it's three, and this is what you'd use
    Code:
    var all = "1234567890123456789012345678901234567890";
    all = all.substring(0,25).replace(/(\w)\,/g,"$1");
    Great wit and madness are near allied, and fine a line their bounds divide.

  6. #6
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    Wouldn't
    Code:
    all = all.substring(0,25).replace(/(\w)\,/g,"$1");
    be the same as
    Code:
    all = all.substring(0,25).replace(/,/g, '');
    ?

    (BTW: Comma is not a special char so it shouldn't be escaped)
    #define question (2B || !2B)
    HTMLElement and W3C Event Handling in IE
    My JavaScript Library

    Don't PM me about answers to questions. If I don't reply in a thread it's because:
    • You didn't read the message posting guidelines
    • Your code is too unstructured and/or formatted poorly - correcting it is too time consuming
    • I simply don't know the answer

  7. #7
    Join Date
    Aug 2007
    Posts
    3,767
    Comma doesn't have to be escaped, but I prefer to escape all punctuation, it still works.

    replace(/(\w)\,/g,"$1");
    replaces a comma if it's at the end of a word, while the other replaces all commas regardless of where they are. I'm not a hundred percent sure what the person wants, but looking at it again, this is probably what they want.
    Code:
    all = all.replace(/(\w)\,/g,"$1").substring(0,25);
    Great wit and madness are near allied, and fine a line their bounds divide.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles