zoukankan      html  css  js  c++  java
  • 学习之StringBuilder

    1)此类表示值为可变字符序列的类似字符串的对象。之所以说值是可变的,是因为在通过追加、移除、替换或插入字符而创建它后可以对它进行修改。无法继承此类。

    2)命名空间:System.Text
         程序集:mscorlib(在 mscorlib.dll 中)

    3)StringBuilder 的容量是实例在任何给定时间可存储的最大字符数,并且大于或等于实例值的字符串表示形式的长度。容量可通过Capacity 属性或EnsureCapacity 方法来增加          或减少,但它不能小于 Length属性的值。如果在初始化 StringBuilder 的实例时没有指定容量或最大容量,则使用特定于实现的默认值16。

    4)String 或 StringBuilder 对象的串联操作的性能取决于内存分配的发生频率。String 串联操作每次都分配内存,而 StringBuilder 串联操作仅当 StringBuilder 对象缓冲区太小      而无法容纳新数据时才分配内存。因此,如果串联固定数量的 String 对象,则 String 类更适合串联操作。这种情况下,编译器甚至会将各个串联操作组合到一个操作中。如果串      联任意数量的字符串,则 StringBuilder 对象更适合串联操作;例如,某个循环对用户输入的任意数量的字符串进行串联。

    5)继承层次结构:  System.Text.StringBuilder

    6)下面的代码示例演示如何调用由 StringBuilder 类定义的多个方法。

     1 using System;
     2 using System.Text;
     3 
     4 public sealed class App 
     5 {
     6     static void Main() 
     7     {
     8         // Create a StringBuilder that expects to hold 50 characters.
     9         // Initialize the StringBuilder with "ABC".
    10         StringBuilder sb = new StringBuilder("ABC", 50);
    11 
    12         // Append three characters (D, E, and F) to the end of the StringBuilder.
    13         sb.Append(new char[] { 'D', 'E', 'F' });
    14 
    15         // Append a format string to the end of the StringBuilder.
    16         sb.AppendFormat("GHI{0}{1}", 'J', 'k');
    17 
    18         // Display the number of characters in the StringBuilder and its string.
    19         Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString());
    20 
    21         // Insert a string at the beginning of the StringBuilder.
    22         sb.Insert(0, "Alphabet: ");
    23 
    24         // Replace all lowercase k's with uppercase K's.
    25         sb.Replace('k', 'K');
    26 
    27         // Display the number of characters in the StringBuilder and its string.
    28         Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString());
    29     }
    30 }
    31 
    32 // This code produces the following output.
    33 //
    34 // 11 chars: ABCDEFGHIJk
    35 // 21 chars: Alphabet: ABCDEFGHIJK
    View Code
  • 相关阅读:
    理解Java中字符流与字节流的区别
    程序员常见面试问题(不定期更新)
    软链接 硬链接
    《大数据时代》读后的思考
    mysql——int(size) 中的size表示什么意思
    Access denied for user ''@'localhost' to
    Effective c++——条款03:尽可能使用const
    http——wireshark抓包分析
    vim——vim配置步骤
    正则表达式和通配符
  • 原文地址:https://www.cnblogs.com/XscapeSpace/p/3777003.html
Copyright © 2011-2022 走看看