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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:38:09+00:00 2026-06-15T18:38:09+00:00

I found a class that does this, and it seems exactly what I need.

  • 0

I found a class that does this, and it seems exactly what I need.

public static class StringUtil
{
  public static String limit(String value, int length)
  {
    StringBuilder buf = new StringBuilder(value);
    if (buf.length() > length)
    {
      buf.setLength(length);
      buf.append("…");
    }

    return buf.toString();
  }
}

I have a string called review, and I use that class this way:

StringUtil.limit(review, 50);

It does not seem to be appending.

For what it’s worth, it is for an Android app. This is being done in a PostExectute method of AsyncTask inside of a ListFragement.

Am I doing this right?

  • 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-06-15T18:38:10+00:00Added an answer on June 15, 2026 at 6:38 pm

    Is the string you’re sending in (review) longer than 50 characters? Note that the function only modifies the result if it’s longer than the limit.

    You also don’t seem to be accepting the return value of the method. This method does NOT modify the argument value (since it can’t), it returns a NEW String with the new value.

    Try

    String newreview = StringUtil.limit( review, 50 );
    System.out.println( newreview );
    

    Ok, you asked for a modification to go to the next space first. I didn’t compile this, but it should be close.

    public static String limit(String value, int length)
      {
        // note to Test this first, so you don't create a Buffer unnecessarily.
        String ret = value;
        if ( value.length() > length ) {
           StringBuilder buf = new StringBuilder(value);
           buf.setLength( length );
           int cur = length;
           while( cur < value.length() && value.charAt(cur) != ' ' ) {
               buf.append( value.charAt(cur) );
           }
           if ( cur == value.length() ) {
              ret = value;  // we copied the whole string, as it turns out.
           } else {
              buf.append( "..." );
              ret = buf.toString();
           }
        }
        return value;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found out this morning that proc.new works in a class initialize method, but
I could write myself a helper class that does this when given a functor,
I found that there are many frameworks that will check the duplicate class name
I'm creating an initialising function for the class 'Room', and found that the program
If there are several methods in the test class, I found that the order
I have a collumn as unique=true.. in Exam class.... I found that because transactions
I found this great class, all works well except... I spcify the output directory
I've looked at this before and found javascript that works for me with Google
this is what i mean, class V3 { public: float x,y,z; }; class V3_
I am working on a pretty large PHP class that does a lot of

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.