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

The Archive Base Latest Questions

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

I have a for loop like this : String myString = 123456789; String prefix

  • 0

I have a for loop like this :

    String myString = "123456789";
    String prefix = null;
    for (int i=6;i>=0;--i)
    {
       prefix = myString.substring(0,i);
       process(prefix);
    }

The problem is that if this block of code is executed N times, N*7 strings will be created (7 strings will be created for each execution – because the substring method creates a new String and this method is called 7 times for each execution).
I’m thinking about how to improve performance in this case. I wanted to know if there is a way to use only one String per execution with this being modified each time. That’s the purpose of the StringBuilder, but i think the StringBuilder doesn’t help in this case :

    String myString = "123456789";
    StringBuilder prefix = new StringBuilder(myString);
    for (int i=6;i>=0;--i)
    {
       prefix.delete(i,prefix.length());
       process(prefix.toString());
    }

In this case, prefix always reference the same StringBuilder object, but the same problem appears at another place because prefix.toString() returns always a new String object.

Any idea?

(I know that the topic has been treated many times. But i’ve done some search and i didn’t find solution, maybe that’s the minimal use of memory?)

Thanks for your help

  • 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-26T09:25:06+00:00Added an answer on May 26, 2026 at 9:25 am

    While it is true that myString.substring(0, i) creates a new String object, please note that this does not copy the underlying character data.

    Before doing anything to this code, I would use a profiler to verify that this is indeed a bottleneck (either in terms of CPU usage, or in terms of strain placed on the garbage collector).

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

Sidebar

Related Questions

I'd like avoid loop I have this : string s = AAAA,12,BBBB,34,CCCCC,56; With Linq,
I have a loop like: String tmp; for(int x = 0; x < 1000000;
I have a loop like this: for i=1:no %some calculations fid = fopen('c:\\out.txt','wt'); %write
I have two nested loop in XSL like this, at this moment I use
I have a loop that runs through a variety of websites and I'd like
I have a variable that is built in loop. Something like: $str = ;
I have a completed string like this N:Pay in Cash++RGI:40++R:200++T:Purchase++IP:N++IS:N++PD:PC++UCP:598.80++UPP:0.00++TCP:598.80++TPP:0.00++QE:1++QS:1++CPC:USD++PPC:Points++D:Y++E:Y++IFE:Y++AD:Y++IR:++MV:++CP:~~ N:ERedemption++RGI:42++R:200++T:Purchase++IP:N++IS:N++PD:PC++UCP:598.80++UPP:0.00++TCP:598.80++TPP:0.00++QE:1++QS:1++CPC:USD++PPC:Points++D:Y++E:Y++IFE:Y++AD:Y++IR:++MV:++CP: this string is
Say I have a simple PHP loop like this one // Bad example $array
i have a string like this: row='saint george 1739 1799 violin concerti g 029
I have a plist like this: <dict> <key>New item</key> <dict> <key>document</key> <string>driving licence</string> <key>Overview</key>

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.