SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trim string on last word

    Greetings, I do some trimming on a string of text to grab the first 300 chars of it (but if it's less than that, I take all of it), now I'm looking for a way to make sure that I'm trimming up to the last space before the 300th character. that way, I'm not ending my trim in the middle of a word or something. any help is appreciated.

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cbiti View Post
    Greetings, I do some trimming on a string of text to grab the first 300 chars of it (but if it's less than that, I take all of it), now I'm looking for a way to make sure that I'm trimming up to the last space before the 300th character. that way, I'm not ending my trim in the middle of a word or something. any help is appreciated.
    Try:
    Code:
    s=str.match(/^.{,300}\b/);
    Tab-indentation is a crime against humanity.


Bookmarks

Posting Permissions

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