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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:16:11+00:00 2026-05-16T14:16:11+00:00

I found String.replaceAll() in Java with regular expression underlying. It works fine in short

  • 0

I found String.replaceAll() in Java with regular expression underlying. It works fine in short string.
But in case of long string, I need a more efficient algorithm instead of String.replaceAll().
Anyone could give advice?
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-16T14:16:12+00:00Added an answer on May 16, 2026 at 2:16 pm

    If you want to do incremental replacement, you can use an explicit appendReplacement/Tail loop to a StringBuffer (unfortunately no StringBuilder overloads as of yet).

    Here’s the idiom from the documentation:

     Pattern p = Pattern.compile(PATTERN);
     Matcher m = p.matcher(INPUT_SOURCE);
    
     StringBuffer sb = new StringBuffer();
     while (m.find()) {
         m.appendReplacement(sb, REPLACEMENT);
     }
     m.appendTail(sb);
    
     System.out.println(sb.toString());
    

    This is pretty much how replaceAll is implemented.

    The benefit of this method is that since you have full control over the replacement iteration, you don’t have to store the entire output in memory at any given time as a potentially long String. You can incrementally build the output, flushing the StringBuffer content to the disk periodically. That is, using this method can be more memory efficient than using replaceAll.

    (You can also do fancy replacements that is not supported by the current replacement syntax, e.g. toUpperCase() transformation).

    Note that there is a request for enhancement for Matcher to be able to append to any Appendable. If granted, not only can you use StringBuilder, but you can also directly replace to e.g. a FileWriter.

    Related questions

    • in matcher.replace method,how to limit replace times?
      • Has example of limited replacement and case transformation
    • StringBuilder and StringBuffer in Java

    See also

    • BugID 5066679: Matcher should make more use of Appendable
      • If granted, this request for enhancement would allow Matcher to append to any Appendable
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a regular expression that can be used with replaceall to replace all
I found an example in MSDN for string to datetime conversion. But it doesn't
i need those two method than j2me doesnt have i have found one replaceall();
I found this on another stack question: //http://stackoverflow.com/questions/3418231/c-replace-part-of-a-string-with-another-string // void replaceAll(std::string& str, const std::string&
I want to use a particular string found in the Values folder into normal
I found a lot of links to validate string if it is a date.
I found it odd that a string method operation on a string object doesn't
I found three ways to cast a variable to String in JavaScript. I searched
I found a code where it declared code like private final static String API_RTN_SUCCESS
I've found a Perl regexp that can check if a string is UTF-8 (the

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.