Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6678951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:18:09+00:00 2026-05-26T04:18:09+00:00

A user enters text as HTML in a form, for example: <p>this is my

  • 0

A user enters text as HTML in a form, for example:

<p>this is my <strong>blog</strong> post, 
very <i>long</i> and written in <b>HTML</b></p>

I want to be able to output only a part of the string ( for example the first 20 characters ) without breaking the HTML structure of the user’s input. In this case:

<p>this is my <strong>blog</strong> post, very <i>l</i>...</p>

which renders as

this is my <strong>blog</strong> post, very <i>lo</i>...

Is there a Java library able to do this, or a simple method to use?

MyLibrary.abbreviateHTML(string,20) ?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:18:10+00:00Added an answer on May 26, 2026 at 4:18 am

    Since it’s not very easy to do this correctly I usually strip all tags and truncate. This gives great control on the text size and appearance which usually needs to be placed in places where you do need control.

    Note that you may find my proposal very conservative and it actually is not a proper answer to your question. But most of the times the alternatives are:

    • strip all tags and truncate
    • provide an alternate content manageable rich text which will serve as the truncated text. This of course only works in the case of CMSes etc

    The reason that truncating HTML would be hard is that you don’t know how truncating would affect the structure of the HTML. How would you truncate in the middle of a <ul> or, even worst, in the middle of a complex <table>?

    So the problem here is that HTML can not only contain content and styling (bold, italics) but also structure (lists, tables, divs etc). So a good and safe implementation would be to strip everything out apart inline “styling” tags (bold, italics etc) and truncate while keeping track of unclosed tags.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to validate the value a user enters in a text box, so
I have a simple HTML Form that allows the user to enter some text
I want to validate a year in the form (XXXX) after the user enters
I have a text box in html form like this '&lt input id=yourinput type=text
php page1 --> below bit is pure html: <form action=page2.php method=post> <input type=text name=name
I have an existing HTML table, part of a form where the user enters
I have a simple HTML form like this: <form name='input' action='thankyou.jsp' method='post'> <p>Email Address:
How would I make a UILabel update as a user enters text in a
Displaying Type here to ... until the user enters text into a TextBox is
I want to build a site where the user can enter text and format

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.