C#的StringBuilder方法

一、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()

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

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

(2)定义

cs 复制代码
//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)实例

cs 复制代码
// 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)定义

cs 复制代码
//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

相关推荐
小珑也要变强26 分钟前
队列基础概念
c语言·开发语言·数据结构·物联网
吃饭只吃七分饱2 小时前
arm开发板通信
arm开发·c#
AI原吾3 小时前
掌握Python-uinput:打造你的输入设备控制大师
开发语言·python·apython-uinput
机器视觉知识推荐、就业指导3 小时前
Qt/C++事件过滤器与控件响应重写的使用、场景的不同
开发语言·数据库·c++·qt
毕设木哥3 小时前
25届计算机专业毕设选题推荐-基于python的二手电子设备交易平台【源码+文档+讲解】
开发语言·python·计算机·django·毕业设计·课程设计·毕设
珞瑜·3 小时前
Matlab R2024B软件安装教程
开发语言·matlab
weixin_455446173 小时前
Python学习的主要知识框架
开发语言·python·学习
孤寂大仙v3 小时前
【C++】STL----list常见用法
开发语言·c++·list
她似晚风般温柔7894 小时前
Uniapp + Vue3 + Vite +Uview + Pinia 分商家实现购物车功能(最新附源码保姆级)
开发语言·javascript·uni-app
咩咩大主教4 小时前
C++基于select和epoll的TCP服务器
linux·服务器·c语言·开发语言·c++·tcp/ip·io多路复用