C#之 StringBuilder Class

String,众所周知,静态的.不可改变,也就是说 immutable. 要想变,也成,瞒天过海,有开销.因此StringBuilder Class的出现,可以动态地修改String,   下面介绍 StringBuilder Constructor :

  • StringBuilder() Initializes a new default instance with a size of 16
  • StringBuilder(int) Initializes a new instance with a capacity of int
  • StringBuilder(int1, int2) Initializes a new instance with a default capacity of int1 and a maximum capacity of int2
  • StringBuilder(string) Initializes a new instance with a default value of string
  • StringBuilder(string, int) Initializes a new instance with a default value of string and a capacity of int
  • StringBuilder(string, int1, int2, int3) Initializes a new instance with a default value starting at position int1 of string, int2 characters long, with a capacity of int3

SampleBuilder.cs 程序

start example
using System;using System.Text;class SampleBuilder{  public static void Main ()  {    StringBuilder sb = new StringBuilder("test string");    int length = 0;    length = sb.Length;    Console.WriteLine("The result is: '{0}'", sb);    Console.WriteLine("The length is: {0}", length);    sb.Length = 4;    length = sb.Length;    Console.WriteLine("The result is: '{0}'", sb);    Console.WriteLine("The length is: {0}", length);    sb.Length = 20;    length = sb.Length;    Console.WriteLine("The result is: '{0}'", sb);    Console.WriteLine("The length is: {0}", length);  }}

输出结果:

C:/>SampleBuilderThe result is: 'test string'The length is: 11The result is: 'test'The length is: 4The result is: 'test        'The length is: 20
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值