动态串StringBuilder>
动态串StringBuilder的长度可在运行期间动态地改变。可以为 StringBuilder指定一定的长度,如果容量超出指定长度限制,StringBuilder会申请更多空间。
动态串StringBuilder>设置StringBuilder容量
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using
System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main( string [] args)
{
StringBuilder sb1 = new StringBuilder( " StringBuilder的练习 " );
// StringBuilder初始化默认长度为16个字符
string outStr = " 演示文字: " + sb1.ToString() + Environment.NewLine + " 长度Length为: " + sb1.Length + Environment.NewLine + " 容量Capacity为: " + sb1.Capacity;
// 确保sb1的容量必须是80。
sb1.EnsureCapacity( 80 );
outStr += Environment.NewLine + " 使用EnsureCapacity现在的容量为: " + sb1.Capacity;
sb1.Capacity = 60 ;
outStr += Environment.NewLine + " 现在的容量为: " + sb1.Capacity;
Console.WriteLine(outStr);
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main( string [] args)
{
StringBuilder sb1 = new StringBuilder( " StringBuilder的练习 " );
// StringBuilder初始化默认长度为16个字符
string outStr = " 演示文字: " + sb1.ToString() + Environment.NewLine + " 长度Length为: " + sb1.Length + Environment.NewLine + " 容量Capacity为: " + sb1.Capacity;
// 确保sb1的容量必须是80。
sb1.EnsureCapacity( 80 );
outStr += Environment.NewLine + " 使用EnsureCapacity现在的容量为: " + sb1.Capacity;
sb1.Capacity = 60 ;
outStr += Environment.NewLine + " 现在的容量为: " + sb1.Capacity;
Console.WriteLine(outStr);
Console.ReadLine();
}
}
}
动态串StringBuilder>追加操作
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using
System;
using System.Collections.Generic;
using System.Text;
namespace AppendandAppendFormat
{
class Program
{
static void Main( string [] args)
{
StringBuilder sb = new StringBuilder();
string str1 = " , " ;
char [] char2 = { ' > ' , ' > ' , ' > ' };
bool strbool = false ;
int i = 13 ;
// Append提供向StringBuilder对象实例的结尾追加指定类型字符串的功能
sb.Append(str1);
sb.Append(char2);
sb.Append(strbool);
sb.Append(i);
sb.AppendLine();
// AppendFormat提供向StringBuilder对象实例的结尾追加指定了 格式化 的字符串的功能。
sb.AppendFormat( " 字符串值{0} " , str1);
sb.AppendFormat( " 字符数组{0} " , char2);
sb.AppendLine();
sb.AppendFormat( " 布尔值{0} " , strbool);
sb.AppendFormat( " 整型值{0:D} " , i);
Console.WriteLine(sb.ToString());
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace AppendandAppendFormat
{
class Program
{
static void Main( string [] args)
{
StringBuilder sb = new StringBuilder();
string str1 = " , " ;
char [] char2 = { ' > ' , ' > ' , ' > ' };
bool strbool = false ;
int i = 13 ;
// Append提供向StringBuilder对象实例的结尾追加指定类型字符串的功能
sb.Append(str1);
sb.Append(char2);
sb.Append(strbool);
sb.Append(i);
sb.AppendLine();
// AppendFormat提供向StringBuilder对象实例的结尾追加指定了 格式化 的字符串的功能。
sb.AppendFormat( " 字符串值{0} " , str1);
sb.AppendFormat( " 字符数组{0} " , char2);
sb.AppendLine();
sb.AppendFormat( " 布尔值{0} " , strbool);
sb.AppendFormat( " 整型值{0:D} " , i);
Console.WriteLine(sb.ToString());
Console.ReadLine();
}
}
}
动态串StringBuilder>插入操作
追加操作只能追加到 StringBuilder的结尾,而插入操作可以指定索引位置插入。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using
System;
using System.Collections.Generic;
using System.Text;
namespace Insert
{
class Program
{
static void Main( string [] args)
{
StringBuilder sb = new StringBuilder();
sb.Insert( 0 , false );
sb.Insert( 1 , " 这是字符串值 " );
sb.Insert( 2 , 123 );
sb.Insert( 4 , new char [] { ' 1 ' , ' 2 ' , ' 3 ' });
Console.WriteLine(sb.ToString());
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Insert
{
class Program
{
static void Main( string [] args)
{
StringBuilder sb = new StringBuilder();
sb.Insert( 0 , false );
sb.Insert( 1 , " 这是字符串值 " );
sb.Insert( 2 , 123 );
sb.Insert( 4 , new char [] { ' 1 ' , ' 2 ' , ' 3 ' });
Console.WriteLine(sb.ToString());
Console.ReadLine();
}
}
}
动态串StringBuilder>删除操作
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using
System;
using System.Collections.Generic;
using System.Text;
namespace Remove
{
class Program
{
static void Main( string [] args)
{
StringBuilder sb = new StringBuilder( " 这是一个需要移除的字串 " );
StringBuilder sb2 = sb.Remove( 0 , 2 );
Console.WriteLine(sb2.ToString());
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Remove
{
class Program
{
static void Main( string [] args)
{
StringBuilder sb = new StringBuilder( " 这是一个需要移除的字串 " );
StringBuilder sb2 = sb.Remove( 0 , 2 );
Console.WriteLine(sb2.ToString());
Console.ReadKey();
}
}
}
动态串StringBuilder>替换
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using
System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main( string [] args)
{
StringBuilder sb = new StringBuilder( " 我的名字叫金三顺 " );
Console.WriteLine(sb.Replace( " 我 " , " 你 " ));
Console.WriteLine(sb.Replace( " 金三顺 " , " 吴必胜 " ));
// 第一个参数是旧字符串,第二个参数是新字符串,第三个是索引位置,第四个是新字符串的长度
Console.WriteLine(sb.Replace( " 名字 " , " 姓名 " , 0 ,sb.Length));
Console.WriteLine(sb.Replace( " 叫 " , " 是 " , 4 , 2 ));
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main( string [] args)
{
StringBuilder sb = new StringBuilder( " 我的名字叫金三顺 " );
Console.WriteLine(sb.Replace( " 我 " , " 你 " ));
Console.WriteLine(sb.Replace( " 金三顺 " , " 吴必胜 " ));
// 第一个参数是旧字符串,第二个参数是新字符串,第三个是索引位置,第四个是新字符串的长度
Console.WriteLine(sb.Replace( " 名字 " , " 姓名 " , 0 ,sb.Length));
Console.WriteLine(sb.Replace( " 叫 " , " 是 " , 4 , 2 ));
Console.ReadLine();
}
}
}