[Unity]StringBuilder

根结点可不就是孤儿發表於2024-07-12

在C#中,字串string還是一種不可變的引用型別,在對字串進行操作修改後,原有的字串記憶體資料會被回收,程式碼所拿到的其實是一個新的字串物件。在需要對字串進行大規模的修改時,會造成GC效能的損耗,C#的標準庫中提供了一個StringBuilder類用來彌補string的這個缺點。

點選檢視程式碼
//Unity的API
 [DefaultMember("Chars")]
    public sealed class StringBuilder : ISerializable
    {
        public StringBuilder();
        public StringBuilder(int capacity);
        public StringBuilder(string value);
        public StringBuilder(int capacity, int maxCapacity);
        public StringBuilder(string value, int capacity);
        public StringBuilder(string value, int startIndex, int length, int capacity);

        public char this[int index] { get; set; }

        public int Capacity { get; set; }
        public int Length { get; set; }
        public int MaxCapacity { get; }

        public StringBuilder Append(char value, int repeatCount);
        [CLSCompliant(false)]
        public StringBuilder Append(char* value, int valueCount);
        public StringBuilder Append(byte value);
        public StringBuilder Append(bool value);
        [CLSCompliant(false)]
        public StringBuilder Append(ulong value);
        [CLSCompliant(false)]
        public StringBuilder Append(uint value);
        [CLSCompliant(false)]
        public StringBuilder Append(ushort value);
        public StringBuilder Append(char value);
        public StringBuilder Append(StringBuilder value);
        public StringBuilder Append(string value, int startIndex, int count);
        public StringBuilder Append(string value);
        public StringBuilder Append(StringBuilder value, int startIndex, int count);
        [CLSCompliant(false)]
        public StringBuilder Append(sbyte value);
        public StringBuilder Append(ReadOnlySpan<char> value);
        public StringBuilder Append(object value);
        public StringBuilder Append(long value);
        public StringBuilder Append(int value);
        public StringBuilder Append(short value);
        public StringBuilder Append(double value);
        public StringBuilder Append(char[] value);
        public StringBuilder Append(char[] value, int startIndex, int charCount);
        public StringBuilder Append(float value);
        public StringBuilder Append(decimal value);
        public StringBuilder AppendFormat(string format, params object[] args);
        public StringBuilder AppendFormat(string format, object arg0, object arg1, object arg2);
        public StringBuilder AppendFormat(IFormatProvider provider, string format, object arg0, object arg1, object arg2);
        public StringBuilder AppendFormat(string format, object arg0);
        public StringBuilder AppendFormat(IFormatProvider provider, string format, params object[] args);
        public StringBuilder AppendFormat(IFormatProvider provider, string format, object arg0, object arg1);
        public StringBuilder AppendFormat(IFormatProvider provider, string format, object arg0);
        public StringBuilder AppendFormat(string format, object arg0, object arg1);
        public StringBuilder AppendJoin<T>(string separator, IEnumerable<T> values);
        public StringBuilder AppendJoin(string separator, params string[] values);
        public StringBuilder AppendJoin(string separator, params object[] values);
        public StringBuilder AppendJoin(char separator, params object[] values);
        public StringBuilder AppendJoin(char separator, params string[] values);
        public StringBuilder AppendJoin<T>(char separator, IEnumerable<T> values);
        public StringBuilder AppendLine();
        public StringBuilder AppendLine(string value);
        public StringBuilder Clear();
        public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count);
        public void CopyTo(int sourceIndex, Span<char> destination, int count);
        public int EnsureCapacity(int capacity);
        public bool Equals(StringBuilder sb);
        public bool Equals(ReadOnlySpan<char> span);
        [CLSCompliant(false)]
        public StringBuilder Insert(int index, ulong value);
        [CLSCompliant(false)]
        public StringBuilder Insert(int index, uint value);
        [CLSCompliant(false)]
        public StringBuilder Insert(int index, ushort value);
        public StringBuilder Insert(int index, string value, int count);
        public StringBuilder Insert(int index, float value);
        [CLSCompliant(false)]
        public StringBuilder Insert(int index, sbyte value);
        public StringBuilder Insert(int index, ReadOnlySpan<char> value);
        public StringBuilder Insert(int index, string value);
        public StringBuilder Insert(int index, int value);
        public StringBuilder Insert(int index, bool value);
        public StringBuilder Insert(int index, byte value);
        public StringBuilder Insert(int index, long value);
        public StringBuilder Insert(int index, char value);
        public StringBuilder Insert(int index, object value);
        public StringBuilder Insert(int index, char[] value, int startIndex, int charCount);
        public StringBuilder Insert(int index, decimal value);
        public StringBuilder Insert(int index, double value);
        public StringBuilder Insert(int index, short value);
        public StringBuilder Insert(int index, char[] value);
        public StringBuilder Remove(int startIndex, int length);
        public StringBuilder Replace(char oldChar, char newChar, int startIndex, int count);
        public StringBuilder Replace(string oldValue, string newValue);
        public StringBuilder Replace(string oldValue, string newValue, int startIndex, int count);
        public StringBuilder Replace(char oldChar, char newChar);
        public override string ToString();
        public string ToString(int startIndex, int length);
    }

從上面的API我們可以看出來StringBuilder具有值型別相容性、增刪改查、文字格式化等功能。當對StringBuilder修改後,返回的是原來的StringBuilder物件,重複使用StringBuilder例項,可以減少不必要的記憶體分配。
StringBuilder的使用也十分簡單,一個簡單的例子:將一個字串陣列的元素拼接成一個字串,並以/分隔

點選檢視程式碼
        List<string> stringArr = new List<string> { "A", "B", "C" };
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < stringArr.Count; i++)
        {
            sb.Append(stringArr[i] + "/");
        }
        string result = sb.ToString();

相關文章