[Unity]StringBuilder

在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();

热门相关:万里情深不负   隐婚99天:首席,请矜持   管家会给你你想要的一切   新书   与你相恋的小时光