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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:12:40+00:00 2026-06-04T04:12:40+00:00

I have to generate a single string containing all the data members of a

  • 0

I have to generate a single string containing all the data members of a class. For example, if a class definition is

class A    
 {
   private String field1;
   private String field2;
   private String field3;
   ...
   ...
 }

Then I want to generate a string that will contain field1, field2 and field3 in that order. However, the additional thing that I want to do is ensure the following rules –

  field1 is of length 20. If it is less than 20, pad it with blank spaces.
  field2 is of length 10. If it is less than 10, pad it with blank spaces.
  field1 is of length 15. If it is less than 15, pad it with blank spaces.
  ...
  ...

I plan to use a helper class to build this string. I want to use a StringBuilder to get the final string. So I have something like –

  StringBuilder builder = new StringBuilder();

  Helper.addString(field1,20,builder);

Now the implmentation of this addString function is what I am concerned about. This function will be called thousands of times for different classes. So I want to make it as efficient as possible. The question is, what is the most efficient way? Currently, I have the following implementation –

 public static void addString(String field, int totalLengthOfField, StringBuilder builder)
 {
    int lengthOfField = field.length();
    int numberOfBlankSpacesToPutAfterString = totalLengthOfField - lengthOfField;

    if(numberOfBlankSpacesToPutAfterString >=0)
      {
        builder.append(field);
        for(int i=1; i<= numberOfBlankSpacesToPutAfterString; i++)
          {
            builder.append(" "); // append a blank space
          }
      }
    else
      {
        // throw exception - field is longer than the maximum allowed length.
      }
 }
  • 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-04T04:12:40+00:00Added an answer on June 4, 2026 at 4:12 am

    Java has the Formatter class that supports creating strings with width definitions for specific fields, much like sprintf() in C. String.format() also uses a Formatter object internally.

    That said, Formatter is not all that fast. You might be better off appending strings manually to a StringBuilder and using a small cache of strings with various number of spaces, to avoid always appending single spaces…

    For example, if you know that the padding size will always be less than 20 spaces, you could just create a String[] table with strings that have 0, 1, 2… spaces and completely skip the loop. If there is no such restriction, you could still append blocks of, say, 20 spaces at once until you reach the necessary padding size.

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

Sidebar

Related Questions

I have used the ASP.NET Hidden field control, allowing me to generate single-item data
We have configured iReport to generate the following graph: The real data points are
I have a single string that contains the command-line parameters to be passed to
I have the following class: namespace Storage.Models { public class AdminDetail { public string
I have to generate labels from a list of user data stored in a
I have to generate XML in the below format <objects> <items> <item =delete> <searchfields>
I have a questions related to SQL (teradata in particular). I have to generate
for the application I am working on I have to generate consolidated report where
I want to call a method, pass it the length and have it generate
I have tried to generate Hello World with openxml sdk. To my surprise when

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.