stringbuilder

Comparison of String and StringBuilder manipulation in terms of memory usage

╄→尐↘猪︶ㄣ 提交于 2019-12-22 08:39:28
问题 According to SCJP Study Guide by KathySierra: The java.lang.StringBuffer and java.lang.StringBuilder classes should be used when you have to make modifications to strings of characters. As we discussed, String objects are immutable, so if you choose to do a lot of manipulations with String objects, you will end up with a lot of abandoned String objects in the String pool To clear out this, I have gone through the code of String class and StringBuilder source here. The simplfied code of String

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

爱⌒轻易说出口 提交于 2019-12-22 04:32:27
问题 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();

Scala StringBuilder

烂漫一生 提交于 2019-12-22 03:54:09
问题 Is there an implicit method to convert scala.collection.mutable.StringBuilder to java.lang.StringBuilder? I am using a Java library (JCommander) in which one of the methods ( usage ) takes a java.jang.StringBuilder argument. 回答1: You can't start with a Scala StringBuilder and then obtain the Java version. You can, however, wrap a java.lang.StringBuilder in the Scala version. So: val jsb = new java.lang.StringBuilder(); val sb = new StringBuilder(jsb); // Do Scala-y stuff with sb JCommander

How can I change this StringBuilder-to-XML code to LINQ-to-XML?

馋奶兔 提交于 2019-12-22 00:34:13
问题 In my application I build an XML file with this code using StringBuilder : StringBuilder sb = new StringBuilder(); sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + Environment.NewLine); sb.Append(String.Format("<{0}>{1}", _pluralCamelNotation, Environment.NewLine)); for (int index = 0; index < 3; index++) { sb.Append(String.Format("\t<{0}>{1}", _singularCamelNotation, Environment.NewLine)); foreach (DataType dataType in _allDataTypes) { sb.Append(String.Format("\t\t<{0}>{2}</{0}>{1}",

Equivalent of StringBuilder for byte arrays

我的未来我决定 提交于 2019-12-21 06:46:43
问题 This is a simple one, and one that I thought would have been answered. I did try to find an answer on here, but didn't come up with anything - so apologies if there is something I have missed. Anyway, is there an equivalent of StringBuilder but for byte arrays? I'm not bothered about all the different overloads of Append() - but I'd like to see Append(byte) and Append(byte[]) . Is there anything around or is it roll-your-own time? 回答1: Would MemoryStream work for you? The interface might not

c#操作串口类

微笑、不失礼 提交于 2019-12-21 04:30:46
先放上串口的一个类,自己编写的,觉得这样好用些。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO.Ports; using System.Collections; using System.IO; using System.Text.RegularExpressions; namespace channel_ratio { public partial class Serial { private SerialPort comm = new SerialPort(); public StringBuilder builder = new StringBuilder();//避免在事件处理方法中反复的创建,定义到外面。接收到的数据 private long receive_count = 0;//接收计数 private long send_count = 0;//发送计数 private bool saveflag = false;/

Android StringBuilder vs String Concatenation

放肆的年华 提交于 2019-12-20 12:03:44
问题 I was reading this documentation page, http://developer.android.com/reference/android/util/Log.html. The section here caught my eye: Tip: Don't forget that when you make a call like Log.v(TAG, "index=" + i); that when you're building the string to pass into Log.d, the compiler uses a StringBuilder and at least three allocations occur: the StringBuilder itself, the buffer, and the String object. Realistically, there is also another buffer allocation and copy, and even more pressure on the gc.

StringBuffer is obsolete?

一世执手 提交于 2019-12-20 10:16:11
问题 In the book "Effective Java", Josh Bloch says that StringBuffer is largely obsolete and should be replaced by the non-synchronized implementation 'StringBuilder' . But in my experience, I've still seen widespread use of the StringBuffer class. Why is the StringBuffer class now obsolete and why should StringBuilder be preferred over StringBuffer except for the increased performance due to non-synchronization? 回答1: It's obsolete in that new code on Java 1.5 should generally use StringBuilder -

How is StringBuffer implementing append function without creating two objects?

随声附和 提交于 2019-12-20 08:40:35
问题 It was an interview question. I was asked to implement the StringBuffer append function. I saw the code after the interview. But I cannot understand how the operation is done with creation of a single object. I am thinking like this. String s = "orange"; s.append("apple"); Here two objects are created. But StringBuilder s = new StringBuilder("Orange"); s.append("apple"); Now here only one object is created. How is Java doing this operation? 回答1: First there is a problem with your question:

Assigning null to variable in finally block [duplicate]

ぐ巨炮叔叔 提交于 2019-12-20 07:52:45
问题 This question already has answers here : Does a finally block always get executed in Java? (47 answers) Closed 5 years ago . The output of the following piece of code is "Test Passed"; can someone explain to me why ? public class Test { public static void main(String args[]) { System.out.println(new Test().print()); } protected StringBuilder print() { StringBuilder builder = new StringBuilder(); try { builder.append("Test "); return builder.append("Passed!!!"); } finally { builder = null; } }