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 ...
随机推荐
- Java学习之字符串的创建
转自:http://lavasoft.blog.51cto.com/62575/80034/ Java字符串类(java.lang.String)是Java中使用最多的类,也是最为特殊的一个类,很多时 ...
- QReadWriteLock上锁容忍的等待时间是多久?
非递归上锁情况下,同一个锁,已经被锁过一次,那么另外一个线程尝试上锁,会被挡在外面,但是会被挡多久呢?挡一分钟会被崩溃,还十分钟,还是永久等待而不崩溃(希望如此)?还是会立刻崩溃?因为不可以重复上锁. ...
- VM Agent 和扩展程序
VM Agent 和扩展程序 - 第 1 部分 Windows Azure基础结构服务最近宣布了一项新功能VM Agent.VMAgent是一个轻量级进程,用于启动由Microsoft或合作伙伴 ...
- delphi调用外部程序打开文件
delphi调用外部程序打开文件 ShellExecute的各种用法 一.利用系统默认的邮件收发器发送电子邮件 Uses ..., ShellAPI; Var lpHwnd: HWND; lpOper ...
- Nitrous.IO融资665万美元 帮助开发者省去配置调试等工作-CSDN.NET
Intro to Computer Science Class Online (CS101) - Udacity Building a Search Engine Nitrous.IO融资665万美元 ...
- Android 五大布局(LinearLayout、FrameLayout、AbsoulteLayout、RelativeLayout、TableLayout )
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- hdu 4738 Caocao's Bridges(2013杭州网络赛丶神坑)
就是求最小权值的桥..不过有好几个坑... 1:原图不连通,ans=0. 2: m<=n^2 显然有重边,重边必然不是桥,处理重边直接add(u, v, INF). 3: 最小桥边权为0的时 ...
- [每日一题] OCP1z0-047 :2013-08-22 正则表达式---[^Ale|ax.r$]'
正确答案:DE 一.Oracle正则表达式的相关知识点 '[^Ale|ax.r$]': ^:匹配行的开始字符 $:匹配行的结束字符 []:方括号表示指定一个匹配列表,该列表匹配列表中显示的任何表达式. ...
- 虎扯:小众玩物 webkit家的滚动条
前面的话:对只有一种浏览器支持的属性,就不要出来秀咱前端同学,就像是早些年手机们的充电口一样,集各家所长,咱今天说的是webkit的滚动条样式,视乎只有webkit支持此项定义,有见识的前辈来辩.这玩 ...
- zoj 1081 (改进的弧长算法)(转)
看到网上除了射线法,很长一段代码之外,看到了一个很简单的算法解决这个问题,特意转了过来 /* 这个算法是源自<计算机图形学基础教程>(孙家广,清华大学出版社),在该书 的48-49页,名字 ...