zy91_C#中StringBuilder类以及char类

1.StringBuilder类

1.1程序代码

static void Main(string[] args)
{
    //1.
    StringBuilder bf1 = new StringBuilder();
    StringBuilder bf2 = new StringBuilder(10);
    StringBuilder bf3 = new StringBuilder("Hello Kitty!");
    StringBuilder bf4 = new StringBuilder("1234567890");
    Console.WriteLine("{0} {1} {2} {3}", bf1.Length, bf2.Length, bf3.Length, bf4.Length);
    Console.WriteLine("{0} {1} {2} {3}", bf1.Capacity,bf2.Capacity, bf3.Capacity,bf4.Capacity);
    //2.
    StringBuilder bf5 = new StringBuilder("Hello Kitty!");
    bf5.EnsureCapacity(28);
    Console.WriteLine(bf5.Length);
    Console.WriteLine(bf5.Capacity);
    bf5.Length = 5;
    bf5.Capacity = 9;
    Console.WriteLine(bf5);
    StringBuilder bf6 = new StringBuilder("Hello Kitty!");
    bf6.Length = 20;
    Console.WriteLine(bf6 + "尾");

    //Apepend()向字符串的尾部添加字符
    StringBuilder bf7 = new StringBuilder("Kitty is ");
    int intValue = 3;
    bf7.Append(intValue);
    string stringValue = " years old";
    bf7.Append(stringValue);
    char charValue = ',';
    bf7.Append(charValue);
    char[] charArray = { ' ', 's', 'h', 'e',' ','i', 's',' ' };
    bf7.Append(charArray);
    double doubleValue = 2.5;
    bf7.Append(doubleValue);
    bf7.Append("Kg. That is ");
    bool boolValue = true;
    bf7.Append(boolValue);
    bf7.Append('!');
    Console.WriteLine(bf7);

    Appende()的Length和Capacity关系
    StringBuilder bf8 = new StringBuilder(new string('a', 15));
    Console.WriteLine("Length={0}\tCapacity={1}",bf8.Length,bf8.Capacity);
    bf8.Append(new string('a', 1));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 1));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 14));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 1));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 1));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 30));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 1));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 1));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 62));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 1));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    bf8.Append(new string('a', 132));
    Console.WriteLine("Length={0}\tCapacity={1}", bf8.Length, bf8.Capacity);
    Console.WriteLine(bf8);

    /AppendFormat()向字符串尾部添加指定的格式的字符
    StringBuilder bf9 = new StringBuilder();
    decimal i = 19.23m;
    decimal j = 73.7m;
    bf9.AppendFormat("{0,8:c2}\n+{1,7:c2}\n---------\n{2,8:c2}",i,j,i+j);
    Console.WriteLine(bf9);

    Insert()在字符串的任何位置插入各种类型的数据
    StringBuilder bf10 = new StringBuilder("Kitty   year old,she is Kg,that is !");
    string stringValue1 = "is";
    bf10.Insert(6,stringValue1);
    int intValue1 = 3;
    bf10.Insert(9,intValue1);
    char charValue1 = 's';
    bf10.Insert(15,charValue1);
    double doubleValue1 = 2.5;
    bf10.Insert(28,doubleValue1);
    bool boolValue1 = true;
    bf10.Insert(42,boolValue1);
    Console.WriteLine(bf10);

    ///Replace()
    StringBuilder bf11 = new StringBuilder("Happy birthday Jack");
    bf11.Replace("Jack", "Kitty");
    Console.WriteLine(bf11);

    Remove()
    StringBuilder bf12 = new StringBuilder("Harvard is a famous university!");
    bf12.Remove(13, 7);
    Console.WriteLine(bf12);
}

2.char类

2.1程序代码

private void button1_Click(object sender, EventArgs e)
{
    char character=Convert.ToChar(textBox1.Text);
    string output = "";
    output += "为数字:" + char.IsDigit(character) + "\r\n";
    output += "为字母:" + char.IsLetter(character) + "\r\n";
    output += "为小写:" + char.IsLower(character) + "\r\n";
    output += "为大写:" + char.IsUpper(character) + "\r\n";
    output += "为标点:" + char.IsPunctuation(character) + "\r\n";
    output += "转大写:" + char.ToUpper(character) + "\r\n";
    output += "转小写:" + char.ToLower(character) + "\r\n";
    textBox2.Text=output;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值