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 6662753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:25:27+00:00 2026-05-26T02:25:27+00:00

I need to trim a String in java so that: The quick brown fox

  • 0

I need to trim a String in java so that:

The quick brown fox jumps over the laz dog.

becomes

The quick brown…

In the example above, I’m trimming to 12 characters. If I just use substring I would get:

The quick br…

I already have a method for doing this using substring, but I wanted to know what is the fastest (most efficient) way to do this because a page may have many trim operations.

The only way I can think off is to split the string on spaces and put it back together until its length passes the given length. Is there an other way? Perhaps a more efficient way in which I can use the same method to do a “soft” trim where I preserve the last word (as shown in the example above) and a hard trim which is pretty much a substring.

Thanks,

  • 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-26T02:25:27+00:00Added an answer on May 26, 2026 at 2:25 am

    Below is a method I use to trim long strings in my webapps.
    The “soft” boolean as you put it, if set to true will preserve the last word.
    This is the most concise way of doing it that I could come up with that uses a StringBuffer which is a lot more efficient than recreating a string which is immutable.

    public static String trimString(String string, int length, boolean soft) {
        if(string == null || string.trim().isEmpty()){
            return string;
        }
    
        StringBuffer sb = new StringBuffer(string);
        int actualLength = length - 3;
        if(sb.length() > actualLength){
            // -3 because we add 3 dots at the end. Returned string length has to be length including the dots.
            if(!soft)
                return escapeHtml(sb.insert(actualLength, "...").substring(0, actualLength+3));
            else {
                int endIndex = sb.indexOf(" ",actualLength);
                return escapeHtml(sb.insert(endIndex,"...").substring(0, endIndex+3));
            }
        }
        return string;
    }
    

    Update

    I’ve changed the code so that the ... is appended in the StringBuffer, this is to prevent needless creations of String implicitly which is slow and wasteful.

    Note: escapeHtml is a static import from apache commons:

    import static org.apache.commons.lang.StringEscapeUtils.escapeHtml;

    You can remove it and the code should work the same.

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

Sidebar

Related Questions

i need to trim a string to its first 100 characters using jquery/javascript. also
I have string like that: string val = 555*324-000 now, I need to remove
I have a 1.2GB file that contains a one line string. What I need
I need to do alot of high-performance case-insensitive string comparisons and realized that my
I need to trim the first n alpha characters from a string. Examples: a123456
I need two functions that take a string and the number of characters to
I need to Trim a String . But I want to remove all the
I need to trim ; from a string inside T-SQL. But only if it
$.trim(value); The above jquery code would trim the text. I need to trim the
Every night I need to trim back a table to only contain the latest

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.