How can we prepend strings with StringBuilder?

后端 未结 12 2146
一整个雨季
一整个雨季 2020-12-24 00:22

I know we can append strings using StringBuilder. Is there a way we can prepend strings (i.e. add strings in front of a string) using StringBuilder

相关标签:
12条回答
  • 2020-12-24 00:27

    You could build the string in reverse and then reverse the result. You incur an O(n) cost instead of an O(n^2) worst case cost.

    0 讨论(0)
  • 2020-12-24 00:29

    Using the insert method with the position parameter set to 0 would be the same as prepending (i.e. inserting at the beginning).

    An example is: varStringBuilder.insert(0, "someThing");

    It works both for C# and Java

    0 讨论(0)
  • 2020-12-24 00:29

    I haven't used it but Ropes For Java Sounds intriguing. The project name is a play on words, use a Rope instead of a String for serious work. Gets around the performance penalty for prepending and other operations. Worth a look, if you're going to be doing a lot of this.

    A rope is a high performance replacement for Strings. The datastructure, described in detail in "Ropes: an Alternative to Strings", provides asymptotically better performance than both String and StringBuffer for common string modifications like prepend, append, delete, and insert. Like Strings, ropes are immutable and therefore well-suited for use in multi-threaded programming.

    0 讨论(0)
  • 2020-12-24 00:30

    Judging from the other comments, there's no standard quick way of doing this. Using StringBuilder's .Insert(0, "text") is approximately only 1-3x as fast as using painfully slow String concatenation (based on >10000 concats), so below is a class to prepend potentially thousands of times quicker!

    I've included some other basic functionality such as append(), subString() and length() etc. Both appends and prepends vary from about twice as fast to 3x slower than StringBuilder appends. Like StringBuilder, the buffer in this class will automatically increase when the text overflows the old buffer size.

    The code has been tested quite a lot, but I can't guarantee it's free of bugs.

    class Prepender
    {
        private char[] c;
        private int growMultiplier;
        public int bufferSize;      // Make public for bug testing
        public int left;            // Make public for bug testing
        public int right;           // Make public for bug testing
        public Prepender(int initialBuffer = 1000, int growMultiplier = 10)
        {
            c = new char[initialBuffer];
            //for (int n = 0; n < initialBuffer; n++) cc[n] = '.';  // For debugging purposes (used fixed width font for testing)
            left = initialBuffer / 2;
            right = initialBuffer / 2;
            bufferSize = initialBuffer;
            this.growMultiplier = growMultiplier;
        }
        public void clear()
        {
            left = bufferSize / 2;
            right = bufferSize / 2;
        }
        public int length()
        {
            return right - left;
        }
    
        private void increaseBuffer()
        {
            int nudge = -bufferSize / 2;
            bufferSize *= growMultiplier;
            nudge += bufferSize / 2;
            char[] tmp = new char[bufferSize];
            for (int n = left; n < right; n++) tmp[n + nudge] = c[n];
            left += nudge;
            right += nudge;
            c = new char[bufferSize];
            //for (int n = 0; n < buffer; n++) cc[n]='.';   // For debugging purposes (used fixed width font for testing)
            for (int n = left; n < right; n++) c[n] = tmp[n];
        }
    
        public void append(string s)
        {
            // If necessary, increase buffer size by growMultiplier
            while (right + s.Length > bufferSize) increaseBuffer();
    
            // Append user input to buffer
            int len = s.Length;
            for (int n = 0; n < len; n++)
            {
                c[right] = s[n];
                right++;
            }
        }
        public void prepend(string s)
        {
            // If necessary, increase buffer size by growMultiplier
            while (left - s.Length < 0) increaseBuffer();               
    
            // Prepend user input to buffer
            int len = s.Length - 1;
            for (int n = len; n > -1; n--)
            {
                left--;
                c[left] = s[n];
            }
        }
        public void truncate(int start, int finish)
        {
            if (start < 0) throw new Exception("Truncation error: Start < 0");
            if (left + finish > right) throw new Exception("Truncation error: Finish > string length");
            if (finish < start) throw new Exception("Truncation error: Finish < start");
    
            //MessageBox.Show(left + " " + right);
    
            right = left + finish;
            left = left + start;
        }
        public string subString(int start, int finish)
        {
            if (start < 0) throw new Exception("Substring error: Start < 0");
            if (left + finish > right) throw new Exception("Substring error: Finish > string length");
            if (finish < start) throw new Exception("Substring error: Finish < start");
            return toString(start,finish);
        }
    
        public override string ToString()
        {
            return new string(c, left, right - left);
            //return new string(cc, 0, buffer);     // For debugging purposes (used fixed width font for testing)
        }
        private string toString(int start, int finish)
        {
            return new string(c, left+start, finish-start );
            //return new string(cc, 0, buffer);     // For debugging purposes (used fixed width font for testing)
        }
    }
    
    0 讨论(0)
  • 2020-12-24 00:35

    Prepending a String will usually require copying everything after the insertion point back some in the backing array, so it won't be as quick as appending to the end.

    But you can do it like this in Java (in C# it's the same, but the method is called Insert):

    aStringBuilder.insert(0, "newText");
    
    0 讨论(0)
  • 2020-12-24 00:35

    If you require high performance with lots of prepends, you'll need to write your own version of StringBuilder (or use someone else's). With the standard StringBuilder (although technically it could be implemented differently) insert require copying data after the insertion point. Inserting n piece of text can take O(n^2) time.

    A naive approach would be to add an offset into the backing char[] buffer as well as the length. When there is not enough room for a prepend, move the data up by more than is strictly necessary. This can bring performance back down to O(n log n) (I think). A more refined approach is to make the buffer cyclic. In that way the spare space at both ends of the array becomes contiguous.

    0 讨论(0)
提交回复
热议问题