本文介绍: /将此实例中出现的所有指定字符替换为其他指定字符。参数oldCharChar要替换的字符。newCharChar替换 oldChar 的字符。返回对此实例的引用,其中 oldChar 被 newChar 替换。//将此实例中出现的所有指定字符串的替换为其他指定字符串。newValue);参数oldValueString要替换的字符串。newValueString替换 oldValue 的字符串或 null。返回。

一、StringBuilder方法

StringBuilder方法
Append() 向此实例追加指定对象的字符串表示形式。
AppendFormat() 向此实例追加通过处理复合格式字符串(包含零个或更多格式项)而返回的字符串。 每个格式项都由相应的对象自变量的字符串表示形式替换。
AppendJoin()
AppendLine() 将默认的行终止符(或指定字符串的副本和默认的行终止符)追加到此实例的末尾。
Clear() 清空当前stringbuilder对象中所有字母
CopyTo() 将此实例的指定段中的字符复制到目标 Char 范围或Char 数组的指定段中。
EnsureCapacity(Int32) 确保 StringBuilder 的此实例的容量至少是指定值。
Equals()
GetChunks() 返回一个对象,该对象可用于迭代从此 StringBuilder 实例创建的 ReadOnlyMemory<Char>中表示的字符块。
Insert() 将指定对象的字符串表示形式插入到此实例中的指定字符位置。
Remove(Int32, Int32) 将指定范围的字符从此实例中移除。
Replace() 将此实例中出现的所有指定字符或字符串替换为其他的指定字符或字符串。
ToString()  StringBuilder 的值转换为 String

1.StringBuilder.Append()

        向此实例追加指定对象的字符串表示形式。

(1)重载

Append(StringBuilder) 向此实例追加指定的字符串生成器的字符串表示形式。
Append(StringBuilder+AppendInterpolatedStringHandler) 将指定的内插字符串追加到此实例。
Append(UInt32) 向此实例追加指定的 32 位无符号整数的字符串表示形式。
Append(UInt64) 向此实例追加指定的 64 位无符号整数的字符串表示形式。
Append(Char, Int32) 向此实例追加 Unicode 字符的字符串表示形式指定数目的副本。
Append(Char[], Int32, Int32) 向此实例追加指定的 Unicode 字符子数组的字符串表示形式。
Append(IFormatProvider, StringBuilder+AppendInterpolatedStringHandler) 使用指定的格式将指定的内插字符串追加到此实例。
Append(String) 向此实例追加指定字符串的副本。
Append(String, Int32, Int32) 向此实例追加指定子字符串的副本。
Append(StringBuilder, Int32, Int32) 向此实例追加指定字符串生成器中的子字符串副本。
Append(Char*, Int32) 将从指定地址开始的 Unicode 字符数组追加到此实例。
Append(Single) 向此实例追加指定的单精度浮点数的字符串表示形式。
Append(UInt16) 向此实例追加指定的 16 位无符号整数的字符串表示形式。
Append(ReadOnlySpan<Char>) 向此实例追加指定的只读字符范围的字符串表示形式。
Append(SByte) 向此实例追加指定的 8 位有符号整数的字符串表示形式。
Append(Boolean) 向此实例追加指定的布尔值的字符串表示形式。
Append(Byte) 向此实例追加指定的 8 位无符号整数的字符串表示形式。
Append(Char) 将指定的 Char 对象的字符串表示形式追加到此实例。
Append(Char[]) 向此实例追加指定数组中的 Unicode 字符的字符串表示形式。
Append(Double) 向此实例追加指定的双精度浮点数的字符串表示形式。
Append(Decimal) 向此实例追加指定的十进制数的字符串表示形式。
Append(Int32) 向此实例追加指定的 32 位有符号整数的字符串表示形式。
Append(Int64) 向此实例追加指定的 64 位有符号整数的字符串表示形式。
Append(Object) 向此实例追加指定对象的字符串表示形式。
Append(ReadOnlyMemory<Char>) 向此实例追加指定的只读字符内存区域的字符串表示形式。
Append(Int16) 向此实例追加指定的 16 位有符号整数的字符串表示形式。

(2)Append(StringBuilder)

        向此实例追加指定的字符串生成器的字符串表示形式。

public System.Text.StringBuilder Append (System.Text.StringBuilder? value);

参数
value
StringBuilder
要追加的字符串生成器。

返回
StringBuilder
完成追加操作后对此实例的引用。

(3)Append(StringBuilder+AppendInterpolatedStringHandler)

        将指定的内插字符串追加到此实例。 

public System.Text.StringBuilder Append (ref System.Text.StringBuilder.AppendInterpolatedStringHandler handler);

参数
handler
StringBuilder.AppendInterpolatedStringHandler
要追加的内插字符串。

返回
StringBuilder
完成追加操作后对此实例的引用。

(4)Append(UInt32)

        向此实例追加指定的 32 位无符号整数的字符串表示形式。此 API 不符合 CLS。

[System.CLSCompliant(false)]
public System.Text.StringBuilder Append (uint value);

参数
value
UInt32
要追加的值。

返回
StringBuilder
完成追加操作后对此实例的引用。

属性
CLSCompliantAttribute

例外
ArgumentOutOfRangeException
增大此实例的值将超过 MaxCapacity。

        用Append(UInt32) 方法修改此类的现有实例,不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。

        调用 Append(UInt32)UInt32.ToString 方法以获取字符串 value 表示形式。调用方法AppendFormat 控制 value的的格式设置。

// Append(UInt32)
// 此实例的容量会根据需要进行调整
using System.Text;
namespace ConsoleApp10
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            StringBuilder sb = new("The range of a 32-bit unsigned integer: ");
            sb.Append(uint.MinValue).Append(" to ").Append(uint.MaxValue);
            Console.WriteLine(sb);
        }
    }
}
// 运行结果:
// The range of a 32-bit unsigned integer: 0 to 4294967295

(5)Append(UInt64)

        向此实例追加指定的 64 位无符号整数的字符串表示形式。此 API 不符合 CLS。

[System.CLSCompliant(false)]
public System.Text.StringBuilder Append (ulong value);

参数
value
UInt64
要追加的值。

返回
StringBuilder
完成追加操作后对此实例的引用。

属性
CLSCompliantAttribute

例外
ArgumentOutOfRangeException
增大此实例的值将超过 MaxCapacity。

         用Append(UInt64) 方法修改此类的现有实例不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。

        Append(UInt64)方法 调用 UInt64.ToString(IFormatProvider) 方法以获取字符串 value 表示形式。调用AppendFormat方法控制 value的的格式设置。

// Append(UInt64)
// 此实例的容量会根据需要进行调整
using System.Text;
namespace ConsoleApp11
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            StringBuilder sb = new("The range of a 64-bit unsigned integer: ");
            sb.Append(ulong.MinValue).Append(" to ").Append(ulong.MaxValue);
            Console.WriteLine(sb);
        }
    }
}
// 运行结果:
// The range of a 64-bit unsigned integer: 0 to 18446744073709551615

(6)Append(Char, Int32)

        向此实例追加 Unicode 字符的字符串表示形式指定数目的副本。

public System.Text.StringBuilder Append (char value, int repeatCount);

参数
value
Char
要追加的字符。

repeatCount
Int32
要追加 value 的次数。

返回
StringBuilder
完成追加操作后对此实例的引用。

例外
ArgumentOutOfRangeException
repeatCount 小于零。
- 或 -
增大此实例的值将超过 MaxCapacity。

OutOfMemoryException
内存不足。

         用Append(Char, Int32) 方法修改此类的现有实例不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。

//Append(Char, Int32)
using System.Text;

namespace ConsoleApp12
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            decimal value = 1346.19m;
            StringBuilder sb = new();
            sb.Append('*', 5).AppendFormat("{0:C2}", value).Append('*', 5);
            Console.WriteLine(sb);
        }
    }
}
// The example displays the following output:
// *****$1,346.19*****

        此实例的容量会根据需要进行调整。

(7)Append(Char[], Int32, Int32)

         向此实例追加指定的 Unicode 字符子数组的字符串表示形式。

public System.Text.StringBuilder Append (char[]? value, int startIndex, int charCount);

参数
value
Char[]
字符数组。

startIndex
Int32
value 中的起始位置。

charCount
Int32
要追加的字符数。

返回
StringBuilder
完成追加操作后对此实例的引用。

例外
ArgumentNullException
value 为 null,且 startIndex 和 charCount 不为零。

ArgumentOutOfRangeException
charCount 小于零。
或
startIndex 小于零。
- 或 -
startIndex + charCount 大于 value 的长度。
- 或 -
增大此实例的值将超过 MaxCapacity。

        此方法将 value中指定字符范围追加到当前实例。 如果 value 和 nullstartIndexcount 都为零,则不做任何更改。

        Append(Char[], Int32, Int32) 方法修改此类的现有实例不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。

// Append(Char[], Int32, Int32)方法 
using System.Text;
char[] chars = ['a', 'b', 'c', 'd', 'e'];
StringBuilder sb = new();
int startPosition = Array.IndexOf(chars, 'a');
int endPosition = Array.IndexOf(chars, 'c');
if (startPosition >= 0 && endPosition >= 0)
{
    sb.Append("The array from positions ").Append(startPosition).
              Append(" to ").Append(endPosition).Append(" contains ").
              Append(chars, startPosition, endPosition + 1).Append('.');
    Console.WriteLine(sb);
}
// The example displays the following output:
// The array from positions 0 to 2 contains abc.

        此实例的容量会根据需要进行调整。

(8)Append(IFormatProvider, StringBuilder.AppendInterpolatedStringHandler)

        使用指定的格式将指定的内插字符串追加到此实例。

public System.Text.StringBuilder Append (IFormatProvider? provider, ref System.Text.StringBuilder.AppendInterpolatedStringHandler handler);

参数
provider
IFormatProvider
一个提供区域性特定的格式设置信息的对象。

handler
StringBuilder.AppendInterpolatedStringHandler
要追加的内插字符串。

返回
StringBuilder
完成追加操作后对此实例的引用。

(9)Append(String)

        向此实例追加指定字符串的副本。

public System.Text.StringBuilder Append (string? value);

参数
value
String
要追加的字符串。

返回
StringBuilder
完成追加操作后对此实例的引用。

例外
ArgumentOutOfRangeException
增大此实例的值将超过 MaxCapacity。

        Append(String)  方法修改此类的现有实例不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。

         如果 value 为 null,则不进行更改。

// Append(String)方法 
using System.Text;

namespace ConsoleApp14
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            bool flag = false;
            StringBuilder sb = new();
            sb.Append("The value of the flag is ").Append(flag).Append('.');
            Console.WriteLine(sb.ToString());
        }
    }
}
// The example displays the following output:
// The value of the flag is False.

        此实例的容量会根据需要进行调整。

(10)Append(String, Int32, Int32)

        向此实例追加指定子字符串的副本。

public System.Text.StringBuilder Append (string? value, int startIndex, int count);

参数
value
String
包含要追加的子字符串的字符串。

startIndex
Int32
value 中子字符串开始的位置。

count
Int32
要追加的 value 中的字符数。

返回
StringBuilder
完成追加操作后对此实例的引用。

例外
ArgumentNullException
value 为 null,且 startIndex 和 count 不为零。

ArgumentOutOfRangeException
count 小于零。
- 或 -
startIndex 小于零。
- 或 -
startIndex + count 大于 value 的长度。
- 或 -
增大此实例的值将超过 MaxCapacity。

        此方法将value中指定字符范围追加到当前实例。 如果 value 和 nullstartIndexcount 都为零,则不做任何更改。 Append(String, Int32, Int32) 方法修改此类的现有实例不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。

// Append(String)方法 
using System.Text;

namespace ConsoleApp15
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            string str = "First;George Washington;1789;1797";
            int index = 0;
            StringBuilder sb = new();
            int length = str.IndexOf(';', index);
            sb.Append(str, index, length).Append(" President of the United States: ");
            index += length + 1;
            length = str.IndexOf(';', index) - index;
            sb.Append(str, index, length).Append(", from ");
            index += length + 1;
            length = str.IndexOf(';', index) - index;
            sb.Append(str, index, length).Append(" to ");
            index += length + 1;
            sb.Append(str, index, str.Length - index);
            Console.WriteLine(sb);
        }
    }
}
// The example displays the following output:
// First President of the United States: George Washington, from 1789 to 1797

        此实例的容量会根据需要进行调整。

(11)Append(StringBuilder, Int32, Int32)

        向此实例追加指定字符串生成器中的子字符串副本。

public System.Text.StringBuilder Append (System.Text.StringBuilder? value, int startIndex, int count);

参数
value
StringBuilder
包含要追加的子字符串的字符串生成器。

startIndex
Int32
value 中子字符串开始的位置。

count
Int32
要追加的 value 中的字符数。

返回
StringBuilder
完成追加操作后对此实例的引用。

(12)Append(Char*, Int32)

        将从指定地址开始的 Unicode 字符数组追加到此实例。此 API 不符合 CLS。

        符合 CLS 的替代方案StringBuilder.Append(Char[], Int32, Int32)

[System.CLSCompliant(false)]
public System.Text.StringBuilder Append (char* value, int valueCount);

参数
value
Char*
指向字符数组的指针。

valueCount
Int32
数组中的字符数。

返回
StringBuilder
完成追加操作后对此实例的引用。

属性
CLSCompliantAttribute
例外
ArgumentOutOfRangeException
valueCount 小于零。
- 或 -
增大此实例的值将超过 MaxCapacity。

NullReferenceException
value 为 null 指针。

        此方法将地址value开头的valueCount字符追加到当前实例。 Append(Char*, Int32) 修改此类的现有实例不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。此实例的容量会根据需要进行调整。

(13)Append(Single)

        向此实例追加指定的单精度浮点数的字符串表示形式。

public System.Text.StringBuilder Append (float value);

参数
value
Single
要追加的值。

返回
StringBuilder
完成追加操作后对此实例的引用。

例外
ArgumentOutOfRangeException
增大此实例的值将超过 MaxCapacity。

        Append(Single) 方法 修改此类的现有实例不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。

        Append(Single) 方法调用 Single.ToString 方法以获取字符串 value 表示形式。调用AppendFormat方法控制value的格式设置。

// Append(Single)
using System.Text;

namespace ConsoleApp16
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            float value = 1034769.47f;
            StringBuilder sb = new();
            sb.Append('*', 5).Append(value).Append('*', 5);
            Console.WriteLine(sb);
        }
    }
}
// The example displays the following output:
// *****1034769.47*****

(14)Append(UInt16)

        向此实例追加指定的 16 位无符号整数的字符串表示形式。此 API 不符合 CLS。

[System.CLSCompliant(false)]
public System.Text.StringBuilder Append (ushort value);

参数
value
UInt16
要追加的值。

返回
StringBuilder
完成追加操作后对此实例的引用。

属性
CLSCompliantAttribute
例外
ArgumentOutOfRangeException
增大此实例的值将超过 MaxCapacity。

        Append(UInt16) 方法 修改此类的现有实例不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。

        Append(UInt16) 方法调用 UInt16.ToString(IFormatProvider) 方法以获取的value字符串表示形式。调用AppendFormat 方法控制value的格式设置。

// Append(UInt16)
using System.Text;

namespace ConsoleApp16
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            float value = 1034769.47f;
            StringBuilder sb = new();
            sb.Append('*', 5).Append(value).Append('*', 5);
            Console.WriteLine(sb);
        }
    }
}
// The example displays the following output:
// *****1034769.47*****

        此实例的容量会根据需要进行调整。

(15)Append(ReadOnlySpan<Char>)

        向此实例追加指定的只读字符范围的字符串表示形式。

public System.Text.StringBuilder Append (ReadOnlySpan<char> value);

参数
value
ReadOnlySpan<Char>
要追加的只读字符范围。

返回
StringBuilder
完成追加操作后对此实例的引用。

(16)Append(SByte)

         向此实例追加指定的 8 位有符号整数的字符串表示形式。此 API 不符合 CLS。

[System.CLSCompliant(false)]
public System.Text.StringBuilder Append (sbyte value);

参数
value
SByte
要追加的值。

返回
StringBuilder
完成追加操作后对此实例的引用。

属性
CLSCompliantAttribute
例外
ArgumentOutOfRangeException
增大此实例的值将超过 MaxCapacity。

        Append(SByte) 方法修改此类的现有实例不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。

        Append(SByte) 方法调用 SByte.ToString(IFormatProvider) 方法以获取字符串 value 表示形式。 调用AppendFormat方法控制value 的格式设置。

// Append(SByte)
using System.Text;

namespace ConsoleApp18
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            StringBuilder sb = new("The range of an 8-bit signed integer: ");
            sb.Append(sbyte.MinValue).Append(" to ").Append(sbyte.MaxValue);
            Console.WriteLine(sb);
        }
    }
}
// The example displays the following output:
// The range of an 8-bit unsigned integer: -128 to 127

        此实例的容量会根据需要进行调整。

(17)Append(Boolean)

        向此实例追加指定的布尔值的字符串表示形式。

public System.Text.StringBuilder Append (bool value);

参数
value
Boolean
要追加的布尔值。

返回
StringBuilder
完成追加操作后对此实例的引用。

例外
ArgumentOutOfRangeException
增大此实例的值将超过 MaxCapacity。

        Append(Boolean) 方法 修改此类的现有实例不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。

        Append(Boolean)方法调用Boolean.ToString() 方法以获取value字符串表示形式。

// Append(Boolean)方法 
using System.Text;

namespace ConsoleApp19
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            bool flag = false;
            StringBuilder sb = new();
            sb.Append("The value of the flag is ").Append(flag).Append('.');
            Console.WriteLine(sb.ToString());
        }
    }
}
// The example displays the following output:
// The value of the flag is False.

        此实例的容量会根据需要进行调整。

(18)Append(Byte)

        向此实例追加指定的 8 位无符号整数的字符串表示形式。

public System.Text.StringBuilder Append (byte value);

参数
value
Byte
要追加的值。

返回
StringBuilder
完成追加操作后对此实例的引用。

例外
ArgumentOutOfRangeException
增大此实例的值将超过 MaxCapacity。

        Append(Byte) 方法 修改此类的现有实例不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。

        Append(Byte) 方法调用ToString(IFormatProvider) 方法以获取字符串value 表示形式。调用AppendFormat方法控制value 的格式设置。

// Append(Byte)方法 
using System.Text;

namespace ConsoleApp20
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            byte[] bytes = [16, 132, 27, 253];
            StringBuilder sb = new();
            foreach (var value in bytes)
                sb.Append(value).Append(' ');

            Console.WriteLine("The byte array: {0}", sb.ToString());
        }
    }
}
// The example displays the following output:
// The byte array: 16 132 27 253

        此实例的容量会根据需要进行调整。

(19)Append(Char)

         将指定的 Char 对象的字符串表示形式追加到此实例。

public System.Text.StringBuilder Append (char value);

参数
value
Char
要追加的 UTF 16 编码的代码单元。

返回
StringBuilder
完成追加操作后对此实例的引用。

例外
ArgumentOutOfRangeException
增大此实例的值将超过 MaxCapacity。

        Append(Char) 方法 修改此类的现有实例不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。

// Append(Char)方法 
using System.Text;

namespace ConsoleApp21
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            string str = "Characters in a string.";
            StringBuilder sb = new();
            foreach (var ch in str)
                sb.Append(" '").Append(ch).Append("' ");

            Console.WriteLine("Characters in the string:");
            Console.WriteLine("  {0}", sb);
        }
    }
}
// The example displays the following output:
// Characters in the string:
//    'C'  'h'  'a'  'r'  'a'  'c'  't'  'e'  'r'  's'  ' '  'i'  'n'  ' '  'a'  ' '  's'  't' 'r'  'i'  'n'  'g'  '.'

        此实例的容量会根据需要进行调整。

(20)Append(Char[])

        向此实例追加指定数组中的 Unicode 字符的字符串表示形式。

public System.Text.StringBuilder Append (char[]? value);

参数
value
Char[]
要追加的字符数组。

返回
StringBuilder
完成追加操作后对此实例的引用。

例外
ArgumentOutOfRangeException
增大此实例的值将超过 MaxCapacity。

        此方法将指定数组中的所有字符追加到当前实例,其顺序与 它们在中显示的 value顺序相同。 如果 value 为 null,则不进行更改。

        Append(Char[]) 方法修改此类的现有实例不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。

// Append(Char[])方法 
using System.Text;

namespace ConsoleApp22
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            char[] chars = ['a', 'e', 'i', 'o', 'u'];
            StringBuilder sb = new();
            sb.Append("The characters in the array: ").Append(chars);
            Console.WriteLine(sb);
        }
    }
}
// The example displays the following output:
// The characters in the array: aeiou

        此实例的容量会根据需要进行调整。

(21)Append(Double)

         向此实例追加指定的双精度浮点数的字符串表示形式。

public System.Text.StringBuilder Append (double value);

参数
value
Double
要追加的值。

返回
StringBuilder
完成追加操作后对此实例的引用。

例外
ArgumentOutOfRangeException
增大此实例的值将超过 MaxCapacity。

        Append(Double) 方法 修改此类的现有实例不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。

        Append(Double) 方法调用 Double.ToString(IFormatProvider) 方法以获取字符串 value 表示形式。调用AppendFormat方法控制value 的格式设置。   

//Append(Double)方法
using System.Text;

namespace ConsoleApp23
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            double value = 1034769.47;
            StringBuilder sb = new();
            sb.Append('*', 5).Append(value).Append('*', 5);
            Console.WriteLine(sb);
        }
    }
}
// The example displays the following output:
// *****1034769.47*****

        此实例的容量会根据需要进行调整。

(22)Append(Decimal)

        向此实例追加指定的十进制数的字符串表示形式。

public System.Text.StringBuilder Append (decimal value);

参数
value
Decimal
要追加的值。

返回
StringBuilder
完成追加操作后对此实例的引用。

例外
ArgumentOutOfRangeException
增大此实例的值将超过 MaxCapacity。

       Append(Decimal)  方法修改此类的现有实例;它不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。

        Append(Decimal) 方法调用 Decimal.ToString(IFormatProvider) 方法以获取字符串 value 表示形式。调用AppendFormat方法控制value的格式设置。

// Append(Decimal)方法 
using System.Text;

namespace ConsoleApp24
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            decimal value = 1346.19m;
            StringBuilder sb = new();
            sb.Append('*', 5).Append(value).Append('*', 5);
            Console.WriteLine(sb);
        }
    }
}
// The example displays the following output:
// *****1346.19*****

        此实例的容量会根据需要进行调整。

(23)Append(Int32)

        向此实例追加指定的 32 位有符号整数的字符串表示形式。

public System.Text.StringBuilder Append (int value);

参数
value
Int32
要追加的值。

返回
StringBuilder
完成追加操作后对此实例的引用。

例外
ArgumentOutOfRangeException
增大此实例的值将超过 MaxCapacity。

        Append(Int32) 方法修改此类的现有实例不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。

         Append(Int32) 方法调用 Int32.ToString(IFormatProvider) 方法以获取字符串 value 表示形式。调用 AppendFormat 方法控制value 的格式 设置。

// Append(Int32) 方法 
using System.Text;

namespace ConsoleApp25
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            StringBuilder sb = new("The range of a 32-bit integer: ");
            sb.Append(int.MinValue).Append(" to ").Append(int.MaxValue);
            Console.WriteLine(sb);
        }
    }
}
// The example displays the following output:
// The range of a 32-bit integer: -2147483648 to 2147483647

        此实例的容量会根据需要进行调整。

(24)Append(Int64)

        向此实例追加指定的 64 位有符号整数的字符串表示形式。

public System.Text.StringBuilder Append (long value);

参数
value
Int64
要追加的值。

返回
StringBuilder
完成追加操作后对此实例的引用。

例外
ArgumentOutOfRangeException
增大此实例的值将超过 MaxCapacity。

        Append(Int64) 方法 修改此类的现有实例;它不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。

        调用Append(Int64) 方法以获取 Int64.ToString(IFormatProvider) 当前 value 字符串表示形式。调用AppendFormat 方法控制value的格式设置。

// Append(Int64)方法 
using System.Text;

namespace ConsoleApp26
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            StringBuilder sb = new("The range of a 64-bit integer: ");
            sb.Append(long.MinValue).Append(" to ").Append(long.MaxValue);
            Console.WriteLine(sb);
        }
    }
}
// The example displays the following output:
//       The range of a 64-bit integer:  -9223372036854775808 to 9223372036854775807

        此实例的容量会根据需要进行调整。

(25)Append(Object)

        向此实例追加指定对象的字符串表示形式。

public System.Text.StringBuilder Append (object? value);

参数
value
Object
要追加的对象。

返回
StringBuilder
完成追加操作后对此实例的引用。

例外
ArgumentOutOfRangeException
增大此实例的值将超过 MaxCapacity。

        Append(Object) 方法修改此类的现有实例不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。 它定义一个 Dog 类,创建一个 Dog 对象,然后对 Append 方法进行三次调用,以创建一个包含狗名称和品种的字符串。

        Append(Object) 方法调用 Object.ToString 方法以获取的 value字符串表示形式。 如果 value 为 null,则不会对 StringBuilder 对象进行更改。

// Append(Object)方法 
using System.Text;

namespace ConsoleApp27
{
    public class Dog(string name, string breed)
    {
        private readonly string breed = breed;
        public string? GetBreed() => breed;
        public string? Name { get; } = name;
        public override string? ToString() => Name;
    }

    public class Example
    {
        public static void Main()
        {
            Dog dog1 = new("Yiska", "Alaskan Malamute");
            StringBuilder sb = new();
            sb.Append(dog1).Append(", Breed: ").Append(dog1.GetBreed());
            Console.WriteLine(sb);
        }
    }
}
// 运行结果:
// Yiska, Breed: Alaskan Malamute

        此实例的容量会根据需要进行调整。

(26)Append(ReadOnlyMemory<Char>)

        向此实例追加指定的只读字符内存区域的字符串表示形式。

public System.Text.StringBuilder Append (ReadOnlyMemory<char> value);

参数
value
ReadOnlyMemory<Char>
要追加的只读字符内存区域。

返回
StringBuilder
完成追加操作后对此实例的引用。

(27)Append(Int16)

        向此实例追加指定的 16 位有符号整数的字符串表示形式。

public System.Text.StringBuilder Append (short value);

参数
value
Int16
要追加的值。

返回
StringBuilder
完成追加操作后对此实例的引用。

例外
ArgumentOutOfRangeException
增大此实例的值将超过 MaxCapacity。

        Append(Int16)方法 修改此类的现有实例不返回新的类实例。 因此,可以对现有引用调用方法或属性,并且不必将返回值分配给 StringBuilder 对象。

        Append(Int16) 方法调用 Int16.ToString(IFormatProvider) 方法以获取字符串 value 表示形式。调用AppendFormat方法控制value的格式设置。

// Append(Int16)方法 
using System.Text;

namespace ConsoleApp28
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            StringBuilder sb = new("The range of a 16-bit integer: ");
            sb.Append(short.MinValue).Append(" to ").Append(short.MaxValue);
            Console.WriteLine(sb);
        }
    }
}
// The example displays the following output:
// The range of a 16-bit integer: -32768 to 32767

        此实例的容量会根据需要进行调整。

2.StringBuilder.AppendFormat()

        向此实例追加通过处理复合格式字符串(包含零个或更多格式项)而返回的字符串。 每个格式项都由相应的对象自变量的字符串表示形式替换。

(1)重载

AppendFormat(String, Object, Object) 向此实例追加通过处理复合格式字符串(包含零个或更多格式项)而返回的字符串。 每个格式项都替换为这两个参数中任意一个参数的字符串表示形式。
AppendFormat(IFormatProvider, String, Object, Object, Object) 向此实例追加通过处理复合格式字符串(包含零个或更多格式项)而返回的字符串。 每个格式项都使用指定的格式提供程序替换为三个参数中任一个的字符串表示形式。
AppendFormat(String, Object, Object, Object) 向此实例追加通过处理复合格式字符串(包含零个或更多格式项)而返回的字符串。 每个格式项都替换为这三个参数中任意一个参数的字符串表示形式。
AppendFormat(IFormatProvider, String, Object, Object) 向此实例追加通过处理复合格式字符串(包含零个或更多格式项)而返回的字符串。 每个格式项都使用指定的格式提供程序替换为两个参数中任一个的字符串表示形式。
AppendFormat(IFormatProvider, CompositeFormat, ReadOnlySpan<Object>) 向此实例追加通过处理复合格式字符串(包含零个或更多格式项)而返回的字符串。 每个格式项都替换为使用指定格式提供程序的任何参数的字符串表示形式。
AppendFormat(String, Object) 向此实例追加通过处理复合格式字符串(包含零个或更多格式项)而返回的字符串。 每个格式项都替换为一个自变量的字符串表示形式。
AppendFormat(IFormatProvider, String, Object[]) 向此实例追加通过处理复合格式字符串(包含零个或更多格式项)而返回的字符串。 每个格式项都使用指定的格式提供程序由参数数组中相应参数的字符串表示形式替换。
AppendFormat(IFormatProvider, String, Object) 向此实例追加通过处理复合格式字符串(包含零个或更多格式项)而返回的字符串。 每个格式项都使用指定的格式提供程序替换为单个参数的字符串表示形式。
AppendFormat(String, Object[]) 向此实例追加通过处理复合格式字符串(包含零个或更多格式项)而返回的字符串。 每个格式项都由参数数组中相应参数的字符串表示形式替换。
AppendFormat(IFormatProvider, CompositeFormat, Object[]) 向此实例追加通过处理复合格式字符串(包含零个或更多格式项)而返回的字符串。 每个格式项都替换为使用指定格式提供程序的任何参数的字符串表示形式。
AppendFormat<TArg0,TArg1,TArg2>(IFormatProvider, CompositeFormat, TArg0, TArg1, TArg2) 向此实例追加通过处理复合格式字符串(包含零个或更多格式项)而返回的字符串。 每个格式项都替换为使用指定格式提供程序的任何参数的字符串表示形式。
AppendFormat<TArg0,TArg1>(IFormatProvider, CompositeFormat, TArg0, TArg1) 向此实例追加通过处理复合格式字符串(包含零个或更多格式项)而返回的字符串。 每个格式项都替换为使用指定格式提供程序的任何参数的字符串表示形式。
AppendFormat<TArg0>(IFormatProvider, CompositeFormat, TArg0) 向此实例追加通过处理复合格式字符串(包含零个或更多格式项)而返回的字符串。 每个格式项都替换为使用指定格式提供程序的任何参数的字符串表示形式。

(2)AppendFormat(String, Object, Object)

        向此实例追加通过处理复合格式字符串(包含零个或更多格式项)而返回的字符串。 每个格式项都替换为这两个参数中任意一个参数的字符串表示形式。

public System.Text.StringBuilder AppendFormat (string format, object? arg0, object? arg1);
参数

format
String
复合格式字符串。

arg0
Object
要设置格式的第一个对象。

arg1
Object
要设置格式的第二个对象。

返回
StringBuilder
对追加了 format 的此实例的引用。 format 中的每个格式项都由相应的对象参数的字符串表示形式替换。

例外
ArgumentNullException
format 为 null。

FormatException
format 无效。
- 或 -
格式项的索引小于 0(零),或者大于等于 2。

ArgumentOutOfRangeException
扩展的字符串的长度将超过 MaxCapacity。
// AppendFormat
using System.Text;
using System.Globalization;

namespace ConsoleApp29
{
    class Sample
    {
        static readonly StringBuilder sb = new();

        public static void Main()
        {
            int var1 = 111;
            float var2 = 2.22F;
            string var3 = "abcd";
            object[] var4 = [3, 4.4, 'X'];

            Console.WriteLine();
            Console.WriteLine("StringBuilder.AppendFormat method:");
            sb.AppendFormat("1) {0}", var1);
            Show(sb);
            sb.AppendFormat("2) {0}, {1}", var1, var2);
            Show(sb);
            sb.AppendFormat("3) {0}, {1}, {2}", var1, var2, var3);
            Show(sb);
            sb.AppendFormat("4) {0}, {1}, {2}", var4);
            Show(sb);
            CultureInfo ci = new("es-ES", true);
            sb.AppendFormat(ci, "5) {0}", var2);
            Show(sb);
        }

        public static void Show(StringBuilder sbs)
        {
            Console.WriteLine(sbs.ToString());
            sb.Length = 0;
        }
    }
}
/*
运行结果:

StringBuilder.AppendFormat method:
1) 111
2) 111, 2.22
3) 111, 2.22, abcd
4) 3, 4.4, X
5) 2,22
*/

(3)AppendFormat(IFormatProvider, String, Object, Object, Object)

        向此实例追加通过处理复合格式字符串(包含零个或更多格式项)而返回的字符串。 每个格式项都使用指定的格式提供程序替换为三个参数中任一个的字符串表示形式。

public System.Text.StringBuilder AppendFormat (IFormatProvider? provider, string format, object? arg0, object? arg1, object? arg2);

参数
provider
IFormatProvider
一个提供区域性特定的格式设置信息的对象。

format
String
复合格式字符串。

arg0
Object
要设置格式的第一个对象。

arg1
Object
要设置格式的第二个对象。

arg2
Object
要设置格式的第三个对象。

返回
StringBuilder
完成追加操作后对此实例的引用。 完成追加操作后,此实例包含执行该操作之前已存在的任何数据,并且有一个 format 的副本作为后缀,其中任何格式规范都由相应对象参数的字符串表示形式替换。

例外
ArgumentNullException
format 为 null。

FormatException
format 无效。
- 或 -
格式项的索引小于 0(零)或者大于或等于 3。

ArgumentOutOfRangeException
扩展的字符串的长度将超过 MaxCapacity。

        使用 AppendFormat(IFormatProvider, String, Object, Object, Object) 方法演示具有整数值的布尔 And 运算的结果。 格式字符串包含六个格式项,但方法的参数列表中只有三个项,因为每个项的格式有两种不同的方式。

// AppendFormat(IFormatProvider, String, Object, Object, Object) 方法
using System.Globalization;
using System.Text;

namespace ConsoleApp30
{
    public class Example
    {
        public static void Main()
        {
            Random rand = new();
            CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-FR");
            StringBuilder sb = new();
            string formatString = "    {0,12:N0} ({0,8:X8})n" +
                                  "And {1,12:N0} ({1,8:X8})n" +
                                  "  = {2,12:N0} ({2,8:X8})n";
            for (int i = 0; i <= 2; i++)
            {
                int value1 = rand.Next();
                int value2 = rand.Next();
                sb.AppendFormat(culture, formatString,
                                value1, value2, value1 & value2).
                                AppendLine();
            }
            Console.WriteLine(sb.ToString());
        }
    }
}
// 运行结果:
//      1 984 112 195 (76432643)
// And 1 179 778 511 (4651FDCF)
//      = 1 178 674 243 (46412443)
//
//     2 034 813 710 (7948CB0E)
// And  569 333 976 (21EF58D8)
//     =  558 385 160 (21484808)
//
//      126 717 735 (078D8F27)
// And 1 830 715 973 (6D1E8245)
//      =   84 705 797 (050C8205)

3.StringBuilder.AppendJoin()

4. StringBuilder.AppendLine()

5. StringBuilder.Clear()

(1) 定义

        从当前 StringBuilder 实例中删除所有字符。

public System.Text.StringBuilder Clear ();

返回值
StringBuilder
长度为 0(零)的对象。

(2)实例

// StringBuilder.Clear 方法
using System.Text;

namespace ConsoleApp32
{
    public class Class1
    {
        public static void Main()
        {
            StringBuilder sb = new("This is a string.");
            Console.WriteLine("{0} ({1} characters)", sb.ToString(), sb.Length);

            sb.Clear();
            Console.WriteLine("{0} ({1} characters)", sb.ToString(), sb.Length);

            sb.Append("This is a second string.");
            Console.WriteLine("{0} ({1} characters)", sb.ToString(), sb.Length);
        }
    }
}
// 运行结果:
// This is a string. (17 characters)
// (0 characters)
// This is a second string. (24 characters)

6. StringBuilder.CopyTo()

(1)重载

CopyTo(Int32, Span<Char>, Int32) 将此实例的指定段中的字符复制到目标 Char 范围。
CopyTo(Int32, Char[], Int32, Int32) 将此实例的指定段中的字符复制到目标 Char 数组的指定段中。

 (2)CopyTo(Int32, Span<Char>, Int32)

        将此实例的指定段中的字符复制到目标 Char 范围。

public void CopyTo (int sourceIndex, Span<char> destination, int count);

参数
sourceIndex
Int32
此实例中开始复制字符的位置。 索引是从零开始的。

destination
Span<Char>
将从中复制字符的可写范围。

count
Int32
要复制的字符数。

        需要有效地将对象的连续部分 StringBuilder 复制到一个范围时,可以使用 CopyTo 方法。

        例如,代码可以使用大量字符填充 StringBuilder 对象,然后使用 CopyTo 方法将对象的连续小段 StringBuilder 复制到处理这些片段的跨度。 处理对象中的所有 StringBuilder 数据时,对象的大小 StringBuilder 将设置为零,并重复循环。

(3)CopyTo(Int32, Char[], Int32, Int32)

        将此实例的指定段中的字符复制到目标 Char 数组的指定段中。

public void CopyTo (int sourceIndex, char[] destination, int destinationIndex, int count);

参数
sourceIndex
Int32
此实例中开始复制字符的位置。 索引是从零开始的。

destination
Char[]
将从中复制字符的数组。

destinationIndex
Int32
destination 中将从其开始复制字符的起始位置。 索引是从零开始的。

count
Int32
要复制的字符数。

例外
ArgumentNullException
destination 为 null。

ArgumentOutOfRangeException
sourceIndex、destinationIndex 或 count 小于零。
- 或 -
sourceIndex 大于此实例的长度。

ArgumentException
sourceIndex + count 大于此实例的长度。
- 或 -
destinationIndex + count 大于 destination 的长度。

        需要有效地将对象的连续部分 StringBuilder 复制到数组中时,可以使用方法。 数组应是固定大小、预分配的、可重用的,并且可能全局可访问。

        例如,代码可以使用大量字符填充 StringBuilder 对象,然后使用 CopyTo 方法将对象的连续小段 StringBuilder 复制到处理这些片段的数组中。 处理对象中的所有 StringBuilder 数据时,对象的大小 StringBuilder 将设置为零,并重复循环。

// CopyTo(Int32, Char[], Int32, Int32) 
// Typically the destination array is small, preallocated, and global while
// the StringBuilder is large with programmatically defined data.
// However, for this example both the array and StringBuilder are small
// and the StringBuilder has predefined data.

using System.Text;

namespace ConsoleApp33
{
    class Sample
    {
        protected static char[] dest = new char[6];
        public static void Main()
        {
            StringBuilder src = new("abcdefghijklmnopqrstuvwxyz!");
            dest[1] = ')';
            dest[2] = ' ';

            // Copy the source to the destination in 9 pieces, 3 characters per piece.
            Console.WriteLine("nPiece) Data:");
            for (int i = 0; i < 9; i++)
            {
                dest[0] = i.ToString()[0];
                src.CopyTo(i * 3, dest, 3, 3);
                Console.Write("    ");
                Console.WriteLine(dest);
            }
        }
    }
}
/*
运行结果:

Piece) Data:
    0) abc
    1) def
    2) ghi
    3) jkl
    4) mno
    5) pqr
    6) stu
    7) vwx
    8) yz!
*/

7.StringBuilder.EnsureCapacity(Int32)

8. StringBuilder.Equals()

9.StringBuilder.GetChunks()

10.StringBuilder.Insert()

        将指定对象的字符串表示形式插入到此实例中的指定字符位置。

(1)重载

Insert(Int32, Char[], Int32, Int32) 将指定的 Unicode 字符子数组的字符串表示形式插入到此实例中的指定字符位置。
Insert(Int32, String, Int32) 将指定字符串的一个或更多副本插入到此实例中的指定字符位置。
Insert(Int32, UInt64)  64 位无符号整数的字符串表示形式插入到此实例中的指定字符位置。
Insert(Int32, UInt32)  32 位无符号整数的字符串表示形式插入到此实例中的指定字符位置。
Insert(Int32, UInt16)  16 位无符号整数的字符串表示形式插入到此实例中的指定字符位置。
Insert(Int32, String) 将字符串插入到此实例中的指定字符位置。
Insert(Int32, Single) 将单精度浮点数的字符串表示形式插入到此实例中的指定字符位置。
Insert(Int32, SByte) 将指定的 8 位带符号整数的字符串表示形式插入到此实例中的指定字符位置。
Insert(Int32, ReadOnlySpan<Char>) 将字符序列插入到此实例中的指定字符位置。
Insert(Int32, Int16) 将指定的 16 位带符号整数的字符串表示形式插入到此实例中的指定字符位置。
Insert(Int32, Int64)  64 位带符号整数的字符串表示形式插入到此实例中的指定字符位置。
Insert(Int32, Int32) 将指定的 32 位带符号整数的字符串表示形式插入到此实例中的指定字符位置。
Insert(Int32, Object) 将对象的字符串表示形式插入到此实例中的指定字符位置。
Insert(Int32, Double) 将双精度浮点数的字符串表示形式插入到此实例中的指定字符位置。
Insert(Int32, Decimal) 将十进制数的字符串表示形式插入到此实例中的指定字符位置。
Insert(Int32, Char[]) 将指定的 Unicode 字符数组的字符串表示形式插入到此实例中的指定字符位置。
Insert(Int32, Char) 将指定的 Unicode 字符的字符串表示形式插入到此实例中的指定位置。
Insert(Int32, Byte) 将指定的 8 位无符号整数的字符串表示形式插入到此实例中的指定字符位置。
Insert(Int32, Boolean) 将布尔值的字符串表示形式插入到此实例中的指定字符位置。

(2)示例

11.StringBuilder.Remove(Int32, Int32)

(1)定义

        将指定范围的字符从此实例中移除。

public System.Text.StringBuilder Remove (int startIndex, int length);

参数
startIndex
Int32
此实例中开始移除操作的从零开始的位置。

length
Int32
要删除的字符数。

返回
StringBuilder
切除操作完成后对此实例的引用。

例外
ArgumentOutOfRangeException
如果 startIndex 或 length 小于零,或者 startIndex + length 大于此实例的长度。

(2)示例

// StringBuilder.Remove(Int32, Int32) 方法
using System.Text;

namespace ConsoleApp34
{
    class Sample
    {
        public static void Main()
        {
            string rule1 = "0----+----1----+----2----+----3----+----4---";
            string rule2 = "01234567890123456789012345678901234567890123";
            string str = "The quick brown fox jumps over the lazy dog.";
            StringBuilder sb = new(str);

            Console.WriteLine();
            Console.WriteLine("StringBuilder.Remove method");
            Console.WriteLine();
            Console.WriteLine("Original value:");
            Console.WriteLine(rule1);
            Console.WriteLine(rule2);
            Console.WriteLine("{0}", sb.ToString());
            Console.WriteLine();

            sb.Remove(10, 6); // Remove "brown "

            Console.WriteLine("New value:");
            Console.WriteLine(rule1);
            Console.WriteLine(rule2);
            Console.WriteLine("{0}", sb.ToString());
        }
    }
}
/*
This example produces the following results:

StringBuilder.Remove method

Original value:
0----+----1----+----2----+----3----+----4---
01234567890123456789012345678901234567890123
The quick brown fox jumps over the lazy dog.

New value:
0----+----1----+----2----+----3----+----4---
01234567890123456789012345678901234567890123
The quick fox jumps over the lazy dog.

*/

12.StringBuilder.Replace()

        将此实例中出现的所有指定字符或字符串替换为其他的指定字符或字符串。

(1)重载

Replace(Char, Char) 将此实例中出现的所有指定字符替换为其他指定字符。
Replace(String, String) 将此实例中出现的所有指定字符串的替换为其他指定字符串。
Replace(Char, Char, Int32, Int32) 将此实例的子字符串中出现的所有指定字符替换为其他指定字符。
Replace(String, String, Int32, Int32) 将此实例的子字符串中出现的所有指定字符串替换为其他指定字符串。

(2)定义

//Replace(Char, Char)
//将此实例中出现的所有指定字符替换为其他指定字符。
public System.Text.StringBuilder Replace (char oldChar, char newChar);

参数
oldChar
Char
要替换的字符。

newChar
Char
替换 oldChar 的字符。

返回
StringBuilder
对此实例的引用,其中 oldChar 被 newChar 替换。

//Replace(String, String)
//将此实例中出现的所有指定字符串的替换为其他指定字符串。
public System.Text.StringBuilder Replace (string oldValue, string? newValue);

参数
oldValue
String
要替换的字符串。

newValue
String
替换 oldValue 的字符串或 null。

返回
StringBuilder
对此实例的引用,其中 oldValue 的所有实例被 newValue 替换。

例外
ArgumentNullException
oldValue 为 null。

ArgumentException
oldValue 的长度为零。

ArgumentOutOfRangeException
增大此实例的值将超过 MaxCapacity。

此方法执行按序号区分大小写的比较,以识别 当前实例中的 匹配项 oldValue 。 如果 newValue 为 null 或 String.Empty,则删除 的所有 oldValue 匹配项。

//Replace(Char, Char, Int32, Int32)
//将此实例的子字符串中出现的所有指定字符替换为其他指定字符。
public System.Text.StringBuilder Replace (char oldChar, char newChar, int startIndex, int count);

参数
oldChar
Char
要替换的字符。

newChar
Char
替换 oldChar 的字符。

startIndex
Int32
此实例中子字符串开始的位置。

count
Int32
子字符串的长度。

返回
StringBuilder
对此实例的引用,其中从 startIndex 到 startIndex + count -1 范围内的 oldChar 被 newChar 替换。

例外
ArgumentOutOfRangeException
startIndex + count 大于此实例的值的长度。
- 或 -
startIndex 或 count 小于零。

此方法执行按序号区分大小写的比较,以识别 当前实例中的 匹配项 oldChar 。 替换后,当前 StringBuilder 对象的大小保持不变。

//Replace(String, String, Int32, Int32)
//将此实例的子字符串中出现的所有指定字符串替换为其他指定字符串。
public System.Text.StringBuilder Replace (string oldValue, string? newValue, int startIndex, int count);

参数
oldValue
String
要替换的字符串。

newValue
String
替换 oldValue 的字符串或 null。

startIndex
Int32
此实例中子字符串开始的位置。

count
Int32
子字符串的长度。

返回
StringBuilder
对此实例的引用,其中从 startIndex 到 startIndex + count - 1 的范围内 oldValue 的所有实例被 newValue 替换。

例外
ArgumentNullException
oldValue 为 null。

ArgumentException
oldValue 的长度为零。

ArgumentOutOfRangeException
startIndex 或 count 小于零。
- 或 -
startIndex 加 count 指示一个不在此实例内的字符位置。
- 或 -
增大此实例的值将超过 MaxCapacity。
此方法执行按序号区分大小写的比较,以识别 在指定子字符串中出现的 oldValue 次数。 如果 newValue 为 null 或 String.Empty,则删除 的所有 oldValue 匹配项。

(3)实例

// StringBuilder.Replace 方法
using System.Text;

namespace ConsoleApp9
{
    class Sample
    {
        public static void Main()
        {
            // 0----+----1----+----2----+----3----+----4---
            // 01234567890123456789012345678901234567890123
            string str = "The quick br!wn d#g jumps #ver the lazy cat.";
            StringBuilder sb = new(str);

            Console.WriteLine();
            Console.WriteLine("StringBuilder.Replace method");
            Console.WriteLine();

            Console.WriteLine("Original value:");
            Show(sb);

            sb.Replace('#', '!', 15, 29);           // Some '#' -> '!'
            Show(sb);
            sb.Replace('!', 'o');                     // All '!' -> 'o'
            Show(sb);
            sb.Replace("cat", "dog");            // All "cat" -> "dog"
            Show(sb);
            sb.Replace("dog", "fox", 15, 20); // Some "dog" -> "fox"

            Console.WriteLine("Final value:");
            Show(sb);
        }

        public static void Show(StringBuilder sbs)
        {
            string rule1 = "0----+----1----+----2----+----3----+----4---";
            string rule2 = "01234567890123456789012345678901234567890123";

            Console.WriteLine(rule1);
            Console.WriteLine(rule2);
            Console.WriteLine("{0}", sbs.ToString());
            Console.WriteLine();
        }
    }
}
/*
This example produces the following results:

StringBuilder.Replace method

Original value:
0----+----1----+----2----+----3----+----4---
01234567890123456789012345678901234567890123
The quick br!wn d#g jumps #ver the lazy cat.

0----+----1----+----2----+----3----+----4---
01234567890123456789012345678901234567890123
The quick br!wn d!g jumps !ver the lazy cat.

0----+----1----+----2----+----3----+----4---
01234567890123456789012345678901234567890123
The quick brown dog jumps over the lazy cat.

0----+----1----+----2----+----3----+----4---
01234567890123456789012345678901234567890123
The quick brown dog jumps over the lazy dog.

Final value:
0----+----1----+----2----+----3----+----4---
01234567890123456789012345678901234567890123
The quick brown fox jumps over the lazy dog.

*/

13.StringBuilder.ToString()

        将 StringBuilder 的值转换为 String。

(1)重载

ToString() 将此实例的值转换为 String
ToString(Int32, Int32) 将此实例中子字符串的值转换为 String

(2)定义

//ToString()
public override string ToString ();

返回
String
其值与此实例相同的字符串。

//ToString(Int32, Int32)
public string ToString (int startIndex, int length);

参数
startIndex
Int32
此实例内子字符串的起始位置。

length
Int32
子字符串的长度。

返回
String
一个字符串,其值与此实例的指定子字符串相同。

例外
ArgumentOutOfRangeException
startIndex 或 length 小于零。
- 或 -
startIndex 和 length 之和大于当前实例的长度。

二、StringBuilder构造器

        详见本文作者的其他文章,C#用StringBuilder高效处理字符串-CSDN博客 https://wenchm.blog.csdn.net/article/details/135397349

三、StringBuilder属性

        详见本文作者的其他文章,C#的StringBuilder属性-CSDN博客         https://blog.csdn.net/wenchm/article/details/135418412

原文地址:https://blog.csdn.net/wenchm/article/details/135419031

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_55496.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注