C# - String与StringBuilder
A String object is called immutable (read-only), because its value cannot be modified after it has been created. Methods that appear to modify
a String object actually return a new String object that contains the modification.
Because strings are immutable, string manipulation routines that perform repeated additions or deletions to what appears to be a single string
can exact a significant performance penalty.
You can use the StringBuilder class instead of the String class
for operations that make multiple changes to the value of a string. Unlike instances of the String class, StringBuilder objects are mutable; when you concatenate,
append, or delete substrings from a string, the operations are performed on a single string.
A StringBuilder object maintains a buffer
to accommodate expansions to the string. New data is appended to the buffer if room is available; otherwise, a new, larger buffer is allocated, data from the original buffer is copied to the new buffer, and the new data is then appended to the new buffer.
Although the StringBuilder class generally
offers better performance than the String class, you should not automatically replace String with StringBuilder whenever you want to manipulate
strings. Performance depends on the size of the string, the amount of memory to be allocated for the new string, the system on which your app is executing, and the type of operation. You should be prepared to test your app to determine whether StringBuilder actually
offers a significant performance improvement.
版权声明:本文博主原创文章。博客,未经同意不得转载。
C# - String与StringBuilder的更多相关文章
- java中 String StringBuffer StringBuilder的区别
* String类是不可变类,只要对String进行修改,都会导致新的对象生成. * StringBuffer和StringBuilder都是可变类,任何对字符串的改变都不会产生新的对象. 在实际使用 ...
- String,StringBuffer,StringBuilder的区别
public static void main(String[] args) { String str = new String("hello...."); StringBuffe ...
- 探秘Java中的String、StringBuilder以及StringBuffer
探秘Java中String.StringBuilder以及StringBuffer 相信String这个类是Java中使用得最频繁的类之一,并且又是各大公司面试喜欢问 到的地方,今天就来和大家一起学习 ...
- String PK StringBuilder,传说就是传说,只有动手实验,才能得出确定的答案
本机测试结果如下: 大部分情况下,string 性能并不比StringBuilder差,只有特殊情况才出现差异,并非 如前面有些朋友测试的结果哪样,只要使用StringBuilder 就一定比Stri ...
- (原)String、StringBuilder、StringBuffer作为形参
今天在刷一道算法题时,突然遇到StringBuilder作为形参和String作为形参时,最终得出来的结果不同.故尝试了几个demo看看它们之间的区别. 当String类型作为参数时, public ...
- 关于String StringBuffer StringBuilder
0. String对象的创建 1.关于类对象的创建,很普通的一种方式就是利用构造器,String类也不例外:String s=new String("Hello world&qu ...
- string与stringBuilder的效率与内存占用实测
using UnityEngine; using System.Diagnostics; using System.Text; using UnityEngine.UI; public class s ...
- C#基础知识系列三(类和结构体、String和StringBuilder、equals和==)
前言 这一节主要来了解一下类和结构体之间的异同点.以及针对String和StringBuilder的用法.equals和==,其实可以看出很多地方都用到了上一节的值类型和引用类型.堆栈和装箱拆箱操作吧 ...
- 探秘Java中String、StringBuilder以及StringBuffer
探秘Java中String.StringBuilder以及StringBuffer 相信String这个类是Java中使用得最频繁的类之一,并且又是各大公司面试喜欢问 到的地方,今天就来和大家一起学习 ...
- String、StringBuilder
public class testString{ public static void main(String[] args) { String a="cool"; String ...
随机推荐
- Struts学习之类型转换
* 从页面中获取对应的内容 * 在动作类action中,声明与页面中表单name属性的值同名的属性 * 提供get和set方法 * struts2框架就会通过 ...
- linux杂记(四)热键[Tab],[ctrl]-c,[ctrl]-d,在线求助man page/info page
[Tab]按键 他具有[命令补全](接在一串指令的第一个字后面)与[档案补齐](接在第一串指令的第二字以后时)的功能.如 [KANO@kelvin ~]$ ca[tab][tab] cabextrac ...
- while 、do...while 、for
1.while 特点:只有条件成立才会执行循环体. while陷阱: while(条件);即直接加分号 2.do while 特点:一定会执行一次循环体 3.for语句 l 初始化等可以是多句(把 ...
- ubantu root 默认密码
安装完Ubuntu后忽然意识到没有设置root密码,不知道密码自然就无法进入根用户下.到网上搜了一下,原来是这麽回事.Ubuntu的默认root密码是随机的,即每次开机都有一个新的root密码.我们可 ...
- table常用
<style> table,table td { border: 1px solid #ccc; border-collapse:collapse; } </style> 注意 ...
- Thread和ExecutorService(一)
ExecutorService是Java并发包里面的一个线程池服务 线程池的优点 复用已有的线程,消除任务中创建线程的开销 通过优化线程的数量,提高性能,保证处理器一直处于忙的状态,而不必创建这么 ...
- cocos android分析
来自:http://xiebaochun.github.io/ cocos2d-x Android环境搭建 cocos2d-x环境搭建比較简单,可是小问题还是不少,我尽量都涵盖的全面一些. 下载软件 ...
- javascript 的工具方法 --- 类型判断
Javascript中常见类型对象有: Boolean, Number, String, Function, Array, Date, RegExp, Object, Error, Symbol等等. ...
- SQL SERVER 2008 R2 自动备份并删除过期备份数据
我们的系统维护的过程中肯定需要对数据库进行定期的备份,但是如果定时手工备份的话,不但浪费时间,也不能保证每次都可以按时备份,所以自动备份成为了我们的不二选择,但是定时备份需要定期清理备份文件, ...
- php制作数据字典
/** * 生成mysql数据字典 */ header("Content-type:text/html;charset=utf-8"); // 配置数据库 $database = ...