一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp.net 简单应用StringBuilder 类

时间:2022-06-25 05:01:12 编辑:袖梨 来源:一聚教程网

stringbuilder 的属性与方法:
--------------------------------------------------------------------------------
 
/* 属性 */
capacity;         // 容量; 可读写, 也可在构建时设置, 但改写时使用 ensurecapacity() 更保险
length;           // 长度; 可读写, 写入 0 时, 同 clear(); length <= capacity <= maxcapacity
maxcapacity;      // 最大容量; 只读, 只能在构建时设置, 超出则异常

/* 方法 */
append();         // 追加; 为接受不同类型的数据, 它有很多重载
appendformat();   // 按格式追加
appendline();     // 追加换行符
clear();          // 取消; 之后 length = 0; 但 capacity、maxcapacity 不变
copyto();         // 复制指定部分到 char[]
ensurecapacity(); // 设置 capacity
insert();         // 插入
remove();         // 移除
replace();        // 替换
tostring();       // 输出文本, 可同时截取

--------------------------------------------------------------------------------

构造函数的 6 种重载:
--------------------------------------------------------------------------------
 

using system.text; // stringbuilder 所属的命名空间

protected void button1_click(object sender, eventargs e)
{
    string str = "";

    /* 无参时, capacity 默认 16 */
    stringbuilder sb1 = new stringbuilder();
    str += sb1.capacity.tostring("capacity: #n"); //capacity: 16

    /* 构建时指定 capacity 大小 */
    stringbuilder sb2 = new stringbuilder(11);
    str += sb2.capacity.tostring("capacity: #n"); //capacity: 11

    /* 使用字符串构建时, 如果字符串 length > 16, 其 capacity 同字符串的长度 */
    stringbuilder sb3 = new stringbuilder("abcdefghijklmnopqrstuvwxyz");
    str += sb3.capacity.tostring("capacity: #n"); //capacity: 26

    /* 指定 capacity 和 maxcapacity */
    stringbuilder sb4 = new stringbuilder(4, 10);
    str += sb4.capacity.tostring("capacity: #t"); //capacity: 4
    sb4.append("1234567890");
    str += sb4.capacity.tostring("capacity: #n"); //capacity: 10
    try { sb4.append("abc"); }
    catch (exception err) { str += err.message + "n"; } //容量小于当前大小...

    /* 用字符串构建, 同时指定 capacity */
    stringbuilder sb5 = new stringbuilder("1234567890", 32);
    str += string.format("capacity: {0}tlength: {1}n", sb5.capacity, sb5.length); //capacity: 32  length: 10

    /* 从字符串截取构建, 同时指定 capacity */
    stringbuilder sb6 = new stringbuilder("abcdefg", 1, 3, 12);
    str += string.format(""{0}"tcapacity: {1}tlength: {2}", sb6, sb6.capacity, sb6.length); //"bcd" capacity: 12 length: 3

    textbox1.text = str;
}

 
--------------------------------------------------------------------------------

练习:
--------------------------------------------------------------------------------
 

//tostring()
protected void button1_click(object sender, eventargs e)
{
    stringbuilder sb = new stringbuilder("1234567890");
    string s1 = sb.tostring();     //1234567890
    string s2 = sb.tostring(2, 3); //345

    textbox1.text = s1 + "n" + s2;
}

//append()、appendformat()、appendline()
protected void button2_click(object sender, eventargs e)
{
    stringbuilder sb = new stringbuilder(256);
    sb.append("abcdefg");
    sb.appendline();
    sb.append(3.1415926);
    sb.appendline();
    sb.appendformat("{0:#.##}", 3.1415926);
    textbox1.text = sb.tostring();
/*
abcdefg
3.1415926
3.14
*/
}

//insert()、remove()、replace()
protected void button3_click(object sender, eventargs e)
{
    stringbuilder sb = new stringbuilder("..........", 256);

    string s1 = sb.insert(2, "abc").tostring();  //..abc........
    string s2 = sb.insert(2, 3.14).tostring();   //..3.14abc........
    string s3 = sb.remove(6, 3).tostring();      //..3.14........
    string s4 = sb.replace('.', '*').tostring(); //**3*14********

    textbox1.text = s1 + "n" + s2 + "n" + s3 + "n" + s4;
}

//copyto
protected void button4_click(object sender, eventargs e)
{
    string str = "..........";
    char[] carr = str.tochararray();

    stringbuilder sb = new stringbuilder("1234567890");
    sb.copyto(2, carr, 0, 3);
    textbox1.text = new string(carr); //345.......
}

热门栏目