一、StringBuilder方法

1.StringBuilder.Append()

2.StringBuilder.AppendFormat()

3.StringBuilder.AppendJoin()

4. StringBuilder.AppendLine()

5. StringBuilder.Clear()

6. StringBuilder.CopyTo()

7.StringBuilder.EnsureCapacity(Int32)

8. StringBuilder.Equals()

9.StringBuilder.GetChunks()

10.StringBuilder.Insert()

11.StringBuilder.Remove(Int32, Int32)

12.StringBuilder.Replace()

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

(1)重载

(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)重载

(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

01-06 12:06