Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. truncate strings

truncate strings

Scheduled Pinned Locked Moved ASP.NET
htmldatabasehelptutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nikneem2005
    wrote on last edited by
    #1

    Hey guys, My website contains a messagebox where visitors can (obviously) post messages. The right side of my website contains an area where the latest three messages are shown. Now I want to truncate these messages to a maximum of 150 characters (if the're shorter, leave them alone). This is easy, not a problem, however, the string can contain image tags (smiley's) now when truncating the string, I want to leave those image tags alone and not truncate in the middle of a tag. I want to remove all html tags (which in fact can only be image tags) then truncate the text to 150 chars as needed and restore the image tags at the original position if needed. Anyone a good approach or idea? I change the image tokens which mean a smiley into image tokens before I store them in the database, this prevents me from checking for image tokens each time I read the message from the database. If I don't convert the tokens into image tags before storing, the problem still exists because a token may be multiple characters (for example a semicolon and a ) sign for a wink smiley) and I don't want to truncate the string in the middle of a token leaving me with a semicolon only.


    I love it when a plan comes together http://www.zonderpunt.nl[^]

    H 1 Reply Last reply
    0
    • N nikneem2005

      Hey guys, My website contains a messagebox where visitors can (obviously) post messages. The right side of my website contains an area where the latest three messages are shown. Now I want to truncate these messages to a maximum of 150 characters (if the're shorter, leave them alone). This is easy, not a problem, however, the string can contain image tags (smiley's) now when truncating the string, I want to leave those image tags alone and not truncate in the middle of a tag. I want to remove all html tags (which in fact can only be image tags) then truncate the text to 150 chars as needed and restore the image tags at the original position if needed. Anyone a good approach or idea? I change the image tokens which mean a smiley into image tokens before I store them in the database, this prevents me from checking for image tokens each time I read the message from the database. If I don't convert the tokens into image tags before storing, the problem still exists because a token may be multiple characters (for example a semicolon and a ) sign for a wink smiley) and I don't want to truncate the string in the middle of a token leaving me with a semicolon only.


      I love it when a plan comes together http://www.zonderpunt.nl[^]

      H Offline
      H Offline
      Harini N K
      wrote on last edited by
      #2

      Hi I have one idea but I am not sure whether it is possible. You need to break words not letters (or any characters) as smileys will not contain spaces between each character. So you can truncate your messages to a maximum number of words. Assume word can have a maximum 30 characters and truncate 10 words maximum. Hope that will be perfect to display in your web pages.

      Rate this message. Thank you. Harini :)

      N 1 Reply Last reply
      0
      • H Harini N K

        Hi I have one idea but I am not sure whether it is possible. You need to break words not letters (or any characters) as smileys will not contain spaces between each character. So you can truncate your messages to a maximum number of words. Assume word can have a maximum 30 characters and truncate 10 words maximum. Hope that will be perfect to display in your web pages.

        Rate this message. Thank you. Harini :)

        N Offline
        N Offline
        nikneem2005
        wrote on last edited by
        #3

        In fact your solution gives my an idea which will furfill my needs... I can count till the desired number of characters, let's say 140 and then truncate the string as soon as the next space occures, or if character 140 is in the middle of a html tag, as soon as the tag ends.... Thanks for bringing up the lighbulb above my head :doh:


        I love it when a plan comes together http://www.zonderpunt.nl[^]

        H 1 Reply Last reply
        0
        • N nikneem2005

          In fact your solution gives my an idea which will furfill my needs... I can count till the desired number of characters, let's say 140 and then truncate the string as soon as the next space occures, or if character 140 is in the middle of a html tag, as soon as the tag ends.... Thanks for bringing up the lighbulb above my head :doh:


          I love it when a plan comes together http://www.zonderpunt.nl[^]

          H Offline
          H Offline
          Harini N K
          wrote on last edited by
          #4

          Yeah that is correct. You are most welcome

          Rate this message. Thank you. Harini :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups