zoukankan      html  css  js  c++  java
  • C# StringBuilder类

      StringBuilder类位于System.Text命名空间下,使用StringBuilder类每次重新生成新字符串时不是再生成一个新实例,而是直接再原来字符串占用的内存看空间上进行处理,而且它可以动态的分配占用内存空间大小。因此在字符串处理操作比较多的情况下,使用StringBuilder类可以大大提高系统的性能。

      默认情况下,编译器会自动为StringBuilder类型的字符串分配一定的内存容量,也可以在程序中直接修改其占用的字节数。

      编写测试代码

      

    using System;
    using System.Text;
    
    namespace StringBuilderExample
    {
        class Program
        {
            static void Main(string[] args)
            {
                StringBuilder str = new StringBuilder();
                Console.WriteLine("字符串:{0},长度{1}", str, str.Length);
                Console.WriteLine("内存容量分配:{0}", str.Capacity);
                str = new StringBuilder("test string.");
                Console.WriteLine("字符串是:{0},长度:{1}", str, str.Length);
                Console.WriteLine("内存容量分配:{0}", str.Capacity);
                str.Append("append another string.");
                Console.WriteLine("字符串是:{0},长度:{1}", str, str.Length);
                Console.WriteLine("内存容量分配:{0}", str.Capacity);
                str = new StringBuilder("test string.", 5);
                Console.WriteLine("字符串是:{0},长度:{1}", str, str.Length);
                Console.WriteLine("内存容量分配:{0}", str.Capacity);
                str = new StringBuilder("test string.", 40);
                Console.WriteLine("字符串是:{0},长度:{1}", str, str.Length);
                Console.WriteLine("内存容量分配:{0}", str.Capacity);
                Console.ReadLine();
            }
        }
    }

    以下是.Net 4.5.2运行结果,版本不同可能会有差别

  • 相关阅读:
    ipv6 for openwrt odhcpd
    openwrt package Makefile
    openwrt 中个网络接口协议说明[转]
    openwrt Package aircrack-ng is missing dependencies for the following libraries:
    linux kernel 从cmdline 提取值
    js 上传文件进度条 [uboot使用]
    printk打印级别 [转]
    linux c 宏定义
    uboot 开发记录
    mac ssh scp命令
  • 原文地址:https://www.cnblogs.com/wintertone/p/11637414.html
Copyright © 2011-2022 走看看