本文整理汇总了C#中System.Text.StringBuilder.Replace方法的典型用法代码示例。如果您正苦于以下问题:C# StringBuilder.Replace方法的具体用法?C# StringBuilder.Replace怎么用?C# StringBuilder.Replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Text.StringBuilder
的用法示例。
在下文中一共展示了StringBuilder.Replace方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Text;
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 StringBuilder(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();
}
}
输出:
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.
示例2: Main
//引入命名空间
using System;
using System.Text;
class UseSBApp {
static void Main(string[] args) {
StringBuilder sb = new StringBuilder("Pineapple");
sb.Replace('e', 'X');
sb.Insert(4, "Banana");
sb.Append("Kiwi");
sb.AppendFormat(", {0}:{1}", 123, 45.6789);
sb.Remove(sb.Length - 3, 3);
Console.WriteLine(sb);
}
}