C# - String与StringBuilder

时间:2022-10-28 10:10:17

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的更多相关文章

  1. java中 String StringBuffer StringBuilder的区别

    * String类是不可变类,只要对String进行修改,都会导致新的对象生成. * StringBuffer和StringBuilder都是可变类,任何对字符串的改变都不会产生新的对象. 在实际使用 ...

  2. String,StringBuffer,StringBuilder的区别

    public static void main(String[] args) { String str = new String("hello...."); StringBuffe ...

  3. 探秘Java中的String、StringBuilder以及StringBuffer

    探秘Java中String.StringBuilder以及StringBuffer 相信String这个类是Java中使用得最频繁的类之一,并且又是各大公司面试喜欢问 到的地方,今天就来和大家一起学习 ...

  4. String PK StringBuilder,传说就是传说,只有动手实验,才能得出确定的答案

    本机测试结果如下: 大部分情况下,string 性能并不比StringBuilder差,只有特殊情况才出现差异,并非 如前面有些朋友测试的结果哪样,只要使用StringBuilder 就一定比Stri ...

  5. (原)String、StringBuilder、StringBuffer作为形参

    今天在刷一道算法题时,突然遇到StringBuilder作为形参和String作为形参时,最终得出来的结果不同.故尝试了几个demo看看它们之间的区别. 当String类型作为参数时, public ...

  6. 关于String StringBuffer StringBuilder

    0. String对象的创建       1.关于类对象的创建,很普通的一种方式就是利用构造器,String类也不例外:String s=new String("Hello world&qu ...

  7. string与stringBuilder的效率与内存占用实测

    using UnityEngine; using System.Diagnostics; using System.Text; using UnityEngine.UI; public class s ...

  8. C#基础知识系列三(类和结构体、String和StringBuilder、equals和==)

    前言 这一节主要来了解一下类和结构体之间的异同点.以及针对String和StringBuilder的用法.equals和==,其实可以看出很多地方都用到了上一节的值类型和引用类型.堆栈和装箱拆箱操作吧 ...

  9. 探秘Java中String、StringBuilder以及StringBuffer

    探秘Java中String.StringBuilder以及StringBuffer 相信String这个类是Java中使用得最频繁的类之一,并且又是各大公司面试喜欢问 到的地方,今天就来和大家一起学习 ...

  10. String、StringBuilder

    public class testString{ public static void main(String[] args) { String a="cool"; String ...

随机推荐

  1. C++ 与 php 的交互 之----- C++ 异步获取 网页文字内容,异步获取 php 的 echo 值。

    已搬迁至 http://www.cnblogs.com/linguanh/p/4543836.html

  2. node学习笔记(四)

    //Node.js标准库提供了http模块,其中封装了一个高效的http服务器和一个简易的http客户端 //http.Server是一个基于事件的HTTP服务器,它的核心由Node.js下层c++部 ...

  3. 怎样更改wordpress登陆 URL防止恶意注册

    WP 默认的登陆 URL 是 wp-login.php或wp-admin.php,许多spamer会根据这些footprint来收集可注册的wordpress站点,然后你的站内就多出许多垃圾评论.如果 ...

  4. shell基础(一)

    Shell本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥梁,用户的大部分工作都是通过Shell完成的.Shell既是一种命令语言,又是一种程序设计语言.作为命令语言,它交互式地解释 ...

  5. 修改jar中的class文件

    在工作中有可能会遇到需要修改jar的情况.正常情况下,如果有源代码的话,就比较容易,直接修改源代码,再导出新的jar文件即可.但是如果没有源代码就比较麻烦了,下面记录了没有源代码的情况修改jar的方法 ...

  6. Java序列化小结

    title: Java序列化小结 date: 2017-05-06 20:07:59 tags: 序列化 categories: Java基础 --- Java序列化就是将一个对象转化成一串二进制表示 ...

  7. 机器学习技法笔记:12 Neural Network

    Roadmap Motivation Neural Network Hypothesis Neural Network Learning Optimization and Regularization ...

  8. rest framework错误笔记——AssertionError: Cannot apply DjangoModelPermissionsOrAnonReadOnly on a view that does not set `.queryset` or have a `.get_queryset()` method.

    用到@api_view装饰器时,访问路由查看api数据时,报错: AssertionError: Cannot apply DjangoModelPermissionsOrAnonReadOnly o ...

  9. TNetHTTPClient 使用

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  10. 2.15 C++常量指针this

    参考: http://www.weixueyuan.net/view/6346.html 总结: 在每一个成员函数中都包含一个常量指针,我们称其为this指针,该指针指向调用本函数的对象,其值为该对象 ...