stringbuilder

关于String的一些问题

你说的曾经没有我的故事 提交于 2019-11-28 00:33:18
1、 String和StringBuilder 什么时候用String?什么时候用StringBuilder? String声明之后在内存中大小是不可修改的,而StringBuilder可以自由扩展大小( String分配在栈区,StringBuilder分配在堆区) 字符串一旦创建就不可修改大小,每次使用System.String类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间。在需要对字符串执行重复修改的情况下,与创建新的String对象相关的系统开销可能会非常昂贵。如果要修改字符串而不创建新的对象,则可以使用System.Text.StringBuilder类。例如当在一个循环中将许多字符串连接在一起时,使用StringBuilder类可以提升性能。 对字符串添加或删除操作比较频繁的话那就用StringBuilder。 2、"==", "Equals", "Object.ReferenceEquals(obj1,obj2)"   1)==它是比较的栈里面的值是否相等(值比较)   2)Equals它比较的是堆里面的值是否相等(引用地址值比较)   3)Object.ReferenceEquals(obj1,obj2)它是比较的是内存地址是否相等 string s1 = "china"; string s2 = "china"; String

How does StringBuilder's capacity change?

家住魔仙堡 提交于 2019-11-28 00:18:37
When I have an empty StringBuilder with a capacity of 5 and I write "hello, world!" to it, does the C# standard specify the new capacity of the StringBuilder ? I have a vague memory that it's twice the new string's length (to avoid changing the capacity with every new appended string). Hans Passant Depends what version of .NET you're talking about. Prior to .NET 4, StringBuilder used the standard .NET strategy , doubling the capacity of the internal buffer every time it needs to be enlarged. StringBuilder was completely rewritten for .NET 4, now using ropes . Extending the allocation is now

浅谈 Java 字符串(String, StringBuffer, StringBuilder)

最后都变了- 提交于 2019-11-27 23:38:59
我们先要记住三者的特征: String 字符串常量 StringBuffer 字符串变量(线程安全) StringBuilder 字符串变量(非线程安全) 一、定义 查看 API 会发现,String、StringBuffer、StringBuilder 都实现了 CharSequence 接口,内部都是用一个char数组实现,虽然它们都与字符串相关,但是其处理机制不同。 String:是不可改变的量,也就是创建后就不能在修改了。 StringBuffer:是一个可变字符串序列,它与 String 一样,在内存中保存的都是一个有序的字符串序列(char 类型的数组),不同点是 StringBuffer 对象的值都是可变的。 StringBuilder:与 StringBuffer 类基本相同,都是可变字符换字符串序列,不同点是 StringBuffer 是线程安全的,StringBuilder 是线程不安全的。 使用场景 使用 String 类的场景:在字符串不经常变化的场景中可以使用 String 类,例如常量的声明、少量的变量运算。 使用 StringBuffer 类的场景:在频繁进行字符串运算(如拼接、替换、删除等),并且运行在多线程环境中,则可以考虑使用 StringBuffer,例如 XML 解析、HTTP 参数解析和封装。 使用 StringBuilder 类的场景

StringBuilder for string concatenation throws OutOfMemoryException

笑着哭i 提交于 2019-11-27 23:34:49
We mostly tend to following the above best practice. Have a look at String vs StringBuilder But StringBuilder could throw OutOfMemoryException even when there is sufficient memory available . It throws OOM exception because it needs "continuous block of memory". Some links for reference StringBuilder OutOfMemoryException and there are many more..... How many of you faced this problem or aware and what did you do to resolve it? Is there anything I am missing? P.S: I wasn't aware of this. I have rephrased the question. *** The same thing worked with manual concatenation(I'll verify this and

String.Format vs “string” + “string” or StringBuilder? [duplicate]

依然范特西╮ 提交于 2019-11-27 23:14:52
问题 This question already has answers here : Closed 10 years ago . Possible Duplicates: Is String.Format as efficient as StringBuilder C# String output: format or concat? What is the performance priority and what should be the conditions to prefer each of the following: String.Format("{0}, {1}", city, state); or city + ", " + state; or StringBuilder sb = new StringBuilder(); sb.Append(city); sb.Append(", "); sb.Append(state); sb.ToString(); 回答1: Compiler will optimize as much string concat as it

C#性能优化总结

梦想的初衷 提交于 2019-11-27 22:52:06
1. C# 语言方面 1.1 垃圾回收 垃圾回收解放了手工管理对象的工作,提高了程序的健壮性,但副作用就是程序代码可能对于对象创建变得随意。 1.1.1 避免不必要的对象创建 由于垃圾回收的代价较高,所以C#程序开发要遵循的一个基本原则就是避免不必要的对象创建。以下列举一些常见的情形。 1.1.1.1 避免循环创建对象 ★ 如果对象并不会随每次循环而改变状态,那么在循环中反复创建对象将带来性能损耗。高效的做法是将对象提到循环外面创建。 1.1.1.2 在需要逻辑分支中创建对象 如果对象只在某些逻辑分支中才被用到,那么应只在该逻辑分支中创建对象。 1.1.1.3 使用常量避免创建对象 程序中不应出现如 new Decimal(0) 之类的代码,这会导致小对象频繁创建及回收,正确的做法是使用Decimal.Zero常量。我们有设计自己的类时,也可以学习这个设计手法,应用到类似的场景中。 1.1.1.4 使用 StringBuilder 做字符串连接 1.1.2 不要使用空析构函数 ★ 如果类包含析构函数,由创建对象时会在 Finalize 队列中添加对象的引用,以保证当对象无法可达时,仍然可以调用到 Finalize 方法。垃圾回收器在运行期间,会启动一个低优先级的线程处理该队列。相比之下,没有析构函数的对象就没有这些消耗。如果析构函数为空,这个消耗就毫无意 义,只会导致性能降低!因此

Is StringBuilder.Replace() more efficient than String.Replace?

一个人想着一个人 提交于 2019-11-27 21:53:11
If you have to use String.Replace() to replace test 50 times, you essentially have to create a new string 50 times. Does StringBuilder.Replace() do this more efficiently? E.g., should I use a StringBuilder if I'm going to be replacing a lot of text, even while I won't be appending any data to it? I'm using .NET, but I assume this would be the same as Java and possibly other languages. This is exactly the type of thing StringBuilder is for - repeated modification of the same text object - it's not just for repeated concatenation, though that appears to be what it's used for most commonly. VonC

2019最新整理JAVA面试题附答案

喜夏-厌秋 提交于 2019-11-27 21:29:34
2019最新整理JAVA面试题附答案! 程序员面试 前天 作者:Zz_maker https://www.cnblogs.com/Zz-maker/p/11193930.html 包含的模块: 本文分为十九个模块,分别是:Java 基础、容器、多线程、反射、对象拷贝、Java Web 、异常、网络、设计模式、Spring/Spring MVC、Spring Boot/Spring Cloud、Hibernate、MyBatis、RabbitMQ、Kafka、Zookeeper、MySQL、Redis、JVM 如下图所示: 共包含 208 道面试题,本文的宗旨是为读者朋友们整理一份详实而又权威的面试清单,下面一起进入主题吧。 ==================================================== 一. Java 基础模块 1.JDK 和 JRE 有什么区别? JDK:Java Development Kit 的简称,Java 开发工具包,提供了 Java 的开发环境和运行环境。 JRE:Java Runtime Environment 的简称,Java 运行环境,为 Java 的运行提供了所需环境。 具体来说 JDK 其实包含了 JRE,同时还包含了编译 Java 源码的编译器 Javac,还包含了很多 Java 程序调试和分析的工具。简单来说

C# LJYZN-105发卡机 NFC读取

房东的猫 提交于 2019-11-27 20:47:03
仅用于“LJYZN-105发卡机”(产品相关资源: http://www.ljyzn.com/product_info.asp?id=202 ) 1) UI 2) Code using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net.Sockets; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ReaderB; namespace NFCReader { public partial class Form1 : Form { //写入配置文件 [DllImport("Kernel32.dll")] public static extern bool WritePrivateProfileString(string strAppName, string strKeyName, string strString, string

Stringbuilder常用方法

╄→гoц情女王★ 提交于 2019-11-27 20:19:26
一、创建Stringbuilder对象 StringBuilder strB = new StringBuilder(); 1、append(String str)/append(Char c):字符串连接 System.out.println("StringBuilder:"+strB.append("ch").append("111").append('c')); //return "StringBuilder:ch111c" 2、toString():返回一个与构建起或缓冲器内容相同的字符串 System.out.println("String:"+strB.toString()); //return "String:ch111c" 3、appendcodePoint(int cp):追加一个代码点,并将其转换为一个或两个代码单元并返回this System.out.println("StringBuilder.appendCodePoint:"+strB.appendCodePoint(2)); //return "StringBuilder.appendCodePoint:ch111c" 4、setCharAt(int i, char c):将第 i 个代码单元设置为 c(可以理解为替换) strB.setCharAt(2, 'd'); System.out