stringbuilder

Why StringBuffer has a toStringCache while StringBuilder not?

无人久伴 提交于 2019-12-05 09:29:04
In JDK 8, StringBuffer class has a toStringCache , while StringBuilder doesn't. /** * A cache of the last value returned by toString. Cleared * whenever the StringBuffer is modified. */ private transient char[] toStringCache; But why? One possible reason I can think of is that StringBuffer is already synchronized so a cache can be implemented easier. Or maybe historically StringBuffer was implemented this way so old code depends heavily on this feature? Given modern JVM with escape analysis and biased locking, is the difference relevant anymore? It might help to consider the historical context

Capitalise first letter in String [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-05 08:44:41
问题 This question already has answers here : How to capitalize the first letter of a String in Java? (51 answers) Closed 6 years ago . I'm having trouble converting the first letter to Capital in a String: rackingSystem.toLowerCase(); // has capitals in every word, so first convert all to lower case StringBuilder rackingSystemSb = new StringBuilder(); rackingSystemSb.append(rackingSystem); rackingSystemSb.setCharAt(0, Character.toUpperCase(rackingSystemSb.charAt(0))); rackingSystem =

替换空格

こ雲淡風輕ζ 提交于 2019-12-05 06:49:51
替换空格 请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。 代码实现 package 剑指offer;/** * @author WangXiaoeZhe * @Date: Created in 2019/11/22 14:50 * @description: */public class Main2 { public static void main(String[] args) { } public String replaceSpace(StringBuffer str){ String s=str.toString(); char[] chars=s.toCharArray(); StringBuilder sb=new StringBuilder(); for (int i=0;i<chars.length;i++){ if (chars[i]==' '){ sb.append("%20"); }else { sb.append(chars[i]); } } return sb.toString(); }} 来源: https://www.cnblogs.com/wuhen8866/p/11911563.html

TextBox.Text += “string”; vs TextBox.AppendText(“string”);

心不动则不痛 提交于 2019-12-05 04:33:53
what is the difference between these two methods? Is one more efficient than the other? I was thinking maybe the AppendText() uses a method similar to the StringBuilder, ie it uses its own cache instead of creating and appending a new string each time, is that true? Thanks. dbw As it is clearly mentioned in Remarks section of MSDN Documentation The AppendText method enables the user to append text to the contents of a text control without using text concatenation, which, can yield better performance when many concatenations are required. Your question, what is the difference between these two

Is using StringBuilder Remove method more memory efficient than creating a new StringBuilder in loop?

不羁岁月 提交于 2019-12-05 04:15:44
In C# which is more memory efficient: Option #1 or Option #2? public void TestStringBuilder() { //potentially a collection with several hundred items: string[] outputStrings = new string[] { "test1", "test2", "test3" }; //Option #1 StringBuilder formattedOutput = new StringBuilder(); foreach (string outputString in outputStrings) { formattedOutput.Append("prefix "); formattedOutput.Append(outputString); formattedOutput.Append(" postfix"); string output = formattedOutput.ToString(); ExistingOutputMethodThatOnlyTakesAString(output); //Clear existing string to make ready for next iteration:

Why isn't string concatenation automatically converted to StringBuilder in C#? [duplicate]

梦想的初衷 提交于 2019-12-05 04:03:06
Possible Duplicate: Why is String.Concat not optimized to StringBuilder.Append? One day I was ranting about a particular Telerik control to a friend of mine. I told him that it took several seconds to generate a controls tree, and after profiling I found out that it is using a string concatenation in a loop instead of a StringBuilder. After rewriting it worked almost instantaneously. So my friend heard that and seemed to be surprised that the C# compiler didn't do that conversion automatically like the Java compiler does. Reading many of Eric Lippert's answers I realize that this feature didn

How much does Java optimize string concatenation with +?

北战南征 提交于 2019-12-05 03:54:58
I know that in more recent Java versions string concatenation String test = one + "two"+ three; Will get optimized to use a StringBuilder . However will a new StringBuilder be generated each time it hits this line or will a single Thread Local StringBuilder be generated that is then used for all string concatenation? In other words can I improve on the performance for a frequently called method by creating my own thread local StringBuilder to re-use or will there be no significant gains by doing so? I can just write a test for this but I wonder if it might be compiler/JVM specific or something

How to print upto two decimal places in java using string builder?

和自甴很熟 提交于 2019-12-05 03:52:53
hi i am trying to print after dividing in string builder and printing that string builder let show me my code , string.append("Memomry usage:total:"+totalMemory/1024/1024+ "Mb-used:"+usageMemory/1024/1024+ " Mb("+Percentage+"%)-free:"+freeMemory/1024/1024+ " Mb("+Percentagefree+"%)"); in above code "totalmemory" and "freememory" is of double type having bytes value in point not null so i divide it by "1024" two times to get it in "Mb" and "string" is variable of string builder after using this code i am simply printing it a am getting result as shown below, Used Memory:Memomry usage: total:13

C#中String 与StringBuilder的区别

让人想犯罪 __ 提交于 2019-12-05 03:09:56
首先要明确一点, String是引用类型, String str=null 。 并且要知道String的值是不可变的。为什么String的值不可变?这个地方我在网上查了一下,暂时没有好的答案,后面找到之后再补充。 String的不可变性举例来说: String a ="123"; a +="45"; 这个时候你可能会以为,a 的值明明变成了"123456".但是实际情况是这样的。第一次string a ="123"; 堆上分配内存,存储值“123”,这里的 a 只是一个内存地址,指向堆上的"123", 当a+="45" 的时候,堆上又分配了一块内存,存放“12345”,这时候a 只是变成了指向"12345"地址的一个对象。所以a 的值每次发生变更,实际上是新增了一个值,之前的"“123”并没有消失。所以在做string拼接字符串的时候,特别是for循环中,使用a+=这种 的语法时,会照成大量的内存损耗。这时候就推荐使用StringBuilder对象了。StringBuilder拼接不会创建新的内存空间。 StringBuilder是一个可以拼接字符串的类,初始化时可以指定stringBuilder对象一个长度,StringBuilder中有一个int类型的Capacity属性,用来指定stringBuilder中容器的长度。StringBuilder提供一个Append

In C#, best way to check if stringbuilder contains a substring

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 01:22:57
I have an existing StringBuilder object, the code appends some values and a delimiter to it. Now I want to modify the code to add the logic that before appending the text I want to check if it really exist in string builder variable or not? If not, then only append otherwise ignore. What is the best way to do so? Do I need to change the object to string type? Need a best approach that would not hamper a performance. public static string BuildUniqueIDList(context RequestContext) { string rtnvalue = string.Empty; try { StringBuilder strUIDList = new StringBuilder(100); for (int iCntr = 0; iCntr