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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:05:47+00:00 2026-05-15T19:05:47+00:00

A legacy app program has a huge String Buffer (size sometimes upto an Mb)

  • 0

A legacy app program has a huge String Buffer (size sometimes upto an Mb) and it is processed sequentially for modifying the contents. I have to implement a change wherein I need to update the string buffer to remove some lines starting with certain specific words. What are the possible ways to implement this ?

Ex:

ABC:djfk kdjf kdsjfk#
ABC:jfue eijf iefe# 
DEL:kdjfi efe eei # 
DEL:ieeif dfddf dfdf#
HJU:heuir fwer ouier# 
ABC:dsf erereree ererre #

I need to delete lines starting with DEL. Splitting the string buffer to string, processing the lines and again joining the strings to create a string buffer would be a bit costly. Pls let me know the possible solutions.

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-15T19:05:48+00:00Added an answer on May 15, 2026 at 7:05 pm

    It is possible to do this in-place efficiently. You’d have to overwrite the characters in the buffer at the proper intervals, and you’d then logically shorten the buffer with setLength. It’s going to be quite complex, but it would be in-place and O(N).

    The reason why you’d want to overwrite instead of using delete/insert is because that would be O(N^2) instead, because things need to be shifted around unnecessarily.

    Doing this out-of-place is quite trivial and O(N) but would require a secondary buffer, doubling the space requirement.


    Proof-of-concept

    Here’s a simple proof-of-concept. removeIntervals takes a StringBuffer and an int[][] intervals. Each int[] is assumed to be a pair of { start, end } values (half-open range, exclusive upper bound). In linear time and in-place, these intervals are removed from the StringBuffer by a simple overwrite. This works when intervals are sorted and non-overlapping, and processed left-to-right.

    The buffer is then shortened with setLength, cutting off as many characters that were removed.

    static void overwrite(StringBuffer sb, int dst, int srcFrom, int srcTo) {
        for (int i = srcFrom; i < srcTo; i++) {
            sb.setCharAt(dst++, sb.charAt(i));
        }
    }
    static int safeGet(int[][] arr, int index, int defaultValue) {
        return (index < arr.length) ? arr[index][0] : defaultValue;
    }
    static void removeIntervals(StringBuffer sb, int[][] intervals) {
        int dst = safeGet(intervals, 0, 0);
        int removed = 0;
        for (int i = 0; i < intervals.length; i++) {
            int start = intervals[i][0];
            int end   = intervals[i][1];
            int nextStart = safeGet(intervals, i+1, sb.length());
            overwrite(sb, dst, end, nextStart);
            removed += end - start;
            dst += nextStart - end;
        }
        sb.setLength(sb.length() - removed);
    }
    

    Then we can test it as follows:

        String text = "01234567890123456789";
        int[][][] tests = {
            { { 0, 5, },
            }, // simple test, removing prefix
            { { 1, 2, }, { 3, 4, }, { 5, 6, }
            }, // multiple infix removals
            { { 3, 7, }, { 18, 20, },
            }, // suffix removal
            {
            }, // no-op
            { { 0, 20 },
            }, // remove whole thing
            { { 7, 10 }, { 10, 13 }, {15, 15 }, 
            }, // adjacent intervals, empty intervals
        };
    
        for (int[][] test : tests) {
            StringBuffer sb = new StringBuffer(text);
            System.out.printf("> '%s'%n", sb);
            System.out.printf("- %s%n", java.util.Arrays.deepToString(test));
            removeIntervals(sb, test);
            System.out.printf("= '%s'%n%n", sb);
        }
    

    This prints (as seen on ideone.com):

    > '01234567890123456789'
    - [[0, 5]]
    = '567890123456789'
    
    > '01234567890123456789'
    - [[1, 2], [3, 4], [5, 6]]
    = '02467890123456789'
    
    > '01234567890123456789'
    - [[3, 7], [18, 20]]
    = '01278901234567'
    
    > '01234567890123456789'
    - []
    = '01234567890123456789'
    
    > '01234567890123456789'
    - [[0, 20]]
    = ''
    
    > '01234567890123456789'
    - [[7, 10], [10, 13], [15, 15]]
    = '01234563456789'
    

    Getting the intervals

    In this specific case, the intervals can either be built in a preliminary pass (using indexOf), or the whole process can be done in one pass if absolutely required. The point is that this can definitely be done in-place in linear time (and if absolutely necessary, in a single-pass).


    An out-of-place solution

    This is out-of-place using a secondary buffer and regex. It’s offered for consideration due to its simplicity. Unless further optimization is provably required (after evidentiary profiling results), this should be good enough:

        String text =
            "DEL: line1\n" +
            "KEP: line2\r\n" +
            "DEL: line3\n" +
            "KEP: line4\r" +
            "DEL: line5\r" +
            "DEL: line6\r" +
            "KEP: line7\n" +
            "DEL: line8";
        StringBuffer sb = new StringBuffer(text);
        Pattern delLine = Pattern.compile("(?m)^DEL:.*$");
        String cleanedUp = delLine.matcher(sb).replaceAll("<deleted!>");
        System.out.println(cleanedUp);
    

    This prints (as seen on ideone.com):

    <deleted!>
    KEP: line2
    <deleted!>
    KEP: line4
    <deleted!>
    <deleted!>
    KEP: line7
    <deleted!>
    

    References

    • regular-expressions.info
    • java.util.regex.Pattern
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 465k
  • Answers 465k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It seems that even with the Enterprise option, you'll still… May 16, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer You can use the following: var values = {}; $("#myform… May 16, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer select extract(epoch from my_timestamp) This returns time in seconds. However,… May 16, 2026 at 1:19 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.