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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:42:40+00:00 2026-06-01T18:42:40+00:00

I have some ‘heavy’ string manipulation in my Java program, which often involves iterating

  • 0

I have some ‘heavy’ string manipulation in my Java program, which often involves iterating through a String and replacing certain segments with filler characters, usually “@”. These are characters are later removed but are used so that the length of the String and the current index are kept intact during the iteration.

This process usually involves replacing more than 1 character at a time.
e.g.
I might need to replace "cat" with "@@@" in the string "I love cats", giving "I love @@@s",
So often I need to create strings of “@” with x length.

In python, this is easy.

NewString = "@" *x

In Java, I find my current method revolting.

String NewString = "";
for (int i=0; i< x; i++)  {  
    NewString = NewString.concat("@");  }

Is there a proper, pre-established method for doing this?
Does anybody have a shorter, more ‘golfed’ method?
Thanks!


Specs:
Java SE (Jre7)
Windows 7 (32)

  • 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-01T18:42:42+00:00Added an answer on June 1, 2026 at 6:42 pm

    It’s not clear to me what kind of regex the comments are suggesting, but creating a string filled with a particular character to the given length is pretty easy:

    public static String createString(char character, int length) {
        char[] chars = new char[length];
        Arrays.fill(chars, character);
        return new String(chars);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some php script updater.php which does some job for 1-2 minutes. Finishing
I have some Hindi text which is not getting displayed on the virtual device.
I have some code in jQuery that iterate through children in div using each().
I have some code dynamically created by Sigma, which looks like this: <div id=myGrid1_headDiv
I have some List for storage data List<List<string>> data = new List<List<string>>(); How to
Have some validating problems which seem to appear only when using the Auth component.
I have some HTML like this (which I cannot change): <div> <p class=name> <span>Employee
Have some Perl code which is using the DBI module - (the code is
I have some code which needs to do things based on a schedule: e.g.
I have some ASP.NET web services which all share a common helper class they

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.