C#中的StringBuilder用法详细指南

2021年3月10日16:02:08 发表评论 1,133 次浏览

C#的StringBuilder类似于Java StringBuilder。一种String对象是不可变的, 即字符串一旦创建就不能更改。每次你使用任何一种System.String类, 然后创建一个新的字符串对象在记忆中。例如, 字符串" lsbin"现在通过更改初始字符串"极客" 至 "玻璃纤维"将在内存堆上创建一个新的字符串对象, 而不是在同一内存位置修改初始字符串。在需要对字符串进行重复修改的情况下, 我们需要StringBuilder类。为了避免字符串替换, 在初始字符串C#中添加, 删除或插入新字符串StringBuilder概念。 StringBuilder是一个动态物体。它不会在内存中创建新对象, 而是动态扩展所需的内存以容纳修改后的字符串或新字符串。

C#中的StringBuilder1

StringBuilder的声明和初始化

可以使用与类相同的方式声明和初始化StringBuilder,

StringBuilder s = new StringBuilder();
            
or

StringBuilder s = new StringBuilder("lsbin");

" s"是的对象StringBuilder类。另外, 我们可以将字符串值(此处为" lsbin")作为参数传递给StringBuilder的构造函数。

定义StringBuilder的容量

虽然StringBuilder是一个动态对象, 它允许你扩展它封装的字符串中的字符数, 你可以为它可以容纳的最大字符数指定一个值。此值称为容量StringBuilder目的。

StringBuilder s = new StringBuilder(20);

or

StringBuilder s = new StringBuilder("lsbin", 20);

这里,

  • 在第一条语句中, 我们将整数值作为参数传递给构造函数。这是可以容纳字符串的最大字符容量。
  • 在第二条语句中, 我们将带有整数值(即字符串可以容纳的最大字符容量)的字符串值作为构造函数的参数。

.

StringBuilder类的重要方法:

  • 追加(字符串值)
  • AppendFormat()
  • 插入(int索引, 字符串值)
  • 删除(int开头, int长度)
  • 替换(旧值, 新值| _val)

StringBuilder.Append(字符串值)方法

的附加方法可用于添加或附加对象的字符串值, 直到当前表示的字符串的末尾StringBuilder目的。AppendLine()方法也属于这种方法。此方法在字符串末尾添加换行符。

例子:

// C# program to demonstrate the 
// StringBuilder.Append(value) and
// StringBuilder.AppendLine(value) method
using System;
using System.Text;
  
class GFG {
  
     // Main Method
     public static void Main()
     {
  
         // "20" is capacity
         StringBuilder s = new StringBuilder( "HELLO " , 20);
          
         s.Append( "GFG" );
  
         // after printing "GEEKS"
         // a new line append
         s.AppendLine( "GEEKS" );
          
         s.Append( "lsbin" );
         Console.WriteLine(s);
     }
}

输出如下:

HELLO GFGGEEKS
lsbin

StringBuilder.AppendFormat()

此方法用于将输入字符串格式化为指定的格式, 然后附加它。此方法还将文本附加到StringBuilder对象的末尾。

// C# program to demonstrate the 
// StringBuilder.AppendFormat() method
using System;
using System.Text;
  
class GFG {
  
     // Main Method
     public static void Main()
     {
         StringBuilder s = new StringBuilder( "Your total amount is " );
  
         // using the method
         s.AppendFormat( "{0:C} " , 50);
  
         Console.WriteLine(s);
     }
}

输出如下:

Your total amount is ¤50.00

StringBuilder.Insert(int index, string value)方法

此方法将字符串插入指定的索引中StringBuilder目的。

例子:

// C# program to demonstrate the 
// StringBuilder.Insert(int index, // string value) method
using System;
using System.Text;
  
class GFG {
  
     // Main Method
     public static void Main()
     {
  
         // "20" is capacity
         StringBuilder s = new StringBuilder( "HELLO " , 20);
          
         // "GEEKS" insert after 6th index
         s.Insert(6, "GEEKS" );
          
         Console.WriteLine(s);
     }
}

输出如下:

HELLO GEEKS

StringBuilder.Remove(int start, int length)方法

此方法从当前StringBuilder对象中删除指定数量的字符。删除过程从指定的索引开始, 一直扩展到另一个指定的索引。

例子:

// C# program to demonstrate the 
// StringBuilder.Remove(int index, // int length) method
using System;
using System.Text;
  
class GFG {
  
     // Main Method
     public static void Main()
     {
  
         // "20" is capacity
         StringBuilder s = new StringBuilder( "lsbin" , 20);
  
         // remove starts from index 5
         // and remove happes 3 index 
         // after index 5
         s.Remove(5, 3);
          
         Console.WriteLine(s);
     }
}

输出如下:

GeeksGeeks

StringBuilder.Replace(old_val, new_val)方法

此方法用于替换StringBuilder具有另一个指定字符的对象。

例子:

// C# program to demonstrate the 
// StringBuilder.Replace(string old_val, // string new_val) method
using System;
using System.Text;
  
class GFG {
  
     // Main Method
     public static void Main()
     {
  
         // "20" is capacity
         StringBuilder s = new StringBuilder( "GFG Geeks " , 20);
          
         // Replace "GFG" with "Geeks For"
         s.Replace( "GFG" , "Geeks For" );
  
         Console.WriteLine(s);
     }
}

输出如下:

Geeks For Geeks

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: