stringbuilder

Mutability of string when string doesn't change in C#

隐身守侯 提交于 2019-11-28 07:05:22
问题 If the string operation doesn't change the value of string, will that end up in creation of a new instance? For example, string str = "foo"; str += ""; I know the difference between string and StringBuilder in C#. 回答1: No, a new instance will be created only when the string operation changes the value in the string variable. It can be proved using the ObjectIDGenerator class. It's worth reading this complete article for proof. using System; using System.Text; using System.Runtime

Java Socket编程

↘锁芯ラ 提交于 2019-11-28 06:33:58
对于Java Socket编程而言,有两个概念,一个是ServerSocket,一个是Socket。服务端和客户端之间通过Socket建立连接,之后它们就可以进行通信了。首先ServerSocket将在服务端监听某个端口,当发现客户端有Socket来试图连接它时,它会accept该Socket的连接请求,同时在服务端建立一个对应的Socket与之进行通信。这样就有两个Socket了,客户端和服务端各一个。 对于Socket之间的通信其实很简单,服务端往Socket的输出流里面写东西,客户端就可以通过Socket的输入流读取对应的内容。Socket与Socket之间是双向连通的,所以客户端也可以往对应的Socket输出流里面写东西,然后服务端对应的Socket的输入流就可以读出对应的内容。下面来看一些服务端与客户端通信的例子: 1、客户端写服务端读 服务端代码 java代码: public class Server { public static void main (String args[]) throws IOException { //为了简单起见,所有的异常信息都往外抛 int port = 8899 ; //定义一个ServerSocket监听在端口8899上 ServerSocket server = new ServerSocket(port); /

Is there any scenario where the Rope data structure is more efficient than a string builder

。_饼干妹妹 提交于 2019-11-28 05:24:26
Related to this question , based on a comment of user Eric Lippert . Is there any scenario where the Rope data structure is more efficient than a string builder? It is some people's opinion that rope data structures are almost never better in terms of speed than the native string or string builder operations in typical cases, so I am curious to see realistic scenarios where indeed ropes are better. The documentation for the SGI C++ implementation goes into some detail on the big O behaviours verses the constant factors which is instructive. Their documentation assumes very long strings being

StringWriter or StringBuilder

放肆的年华 提交于 2019-11-28 04:37:15
What is the difference between StringWriter and StringBuilder and when should I use one or the other? I don't think any of the existing answers really answer the question. The actual relationship between the two classes is an example of the adaptor pattern . StringWriter implements all its Write... methods by forwarding on to an instance of StringBuilder that it stores in a field. This is not merely an internal detail, because StringWriter has a public method GetStringBuilder that returns the internal string builder, and also a constructor that allows you to pass in an existing StringBuilder .

Java面试题目

假装没事ソ 提交于 2019-11-28 04:08:37
String、StringBuffer与StringBuilder的区别 String 字符串常量 StringBuffer 字符串变量(线程安全) StringBuilder 字符串变量(非线程安全) 首先三者主要输在运行速度和线程安全这两位方面。 string:是一个java类,是一个字符串常量,声明是:public final ,所以final的话是改变不了的。字符串值改变不了,就只能在内存创建一个空间来保存新的字符串。所以一旦遇到复杂的操作,用string是多么低效率的事啊。 StringBuilder和StringBuffer有公共类AbstractStringBuilder(抽象类)。 1、执行速度上比较(快慢):stringbuilder>stringbuffer>string 原因(也就是变量和常量的关系):string为字符串常量,后两个是字符串变量,也就是说string对象一旦创建之后该对象是不可更改的,但后两者的对象是变量,是可以更改的。而不进行创建和回收,所以速度要比string快很多。 2、线程安全上比较:stringbuilder是线程不安全的,而stringbuffer是线程安全的。 原因:String:对象定义后,线程安全。 stringbuffer对象在字符串缓冲区被多个线程使用时

Is it better to reuse a StringBuilder in a loop?

杀马特。学长 韩版系。学妹 提交于 2019-11-28 02:50:42
I've a performance related question regarding use of StringBuilder. In a very long loop I'm manipulating a StringBuilder and passing it to another method like this: for (loop condition) { StringBuilder sb = new StringBuilder(); sb.append("some string"); . . . sb.append(anotherString); . . . passToMethod(sb.toString()); } Is instantiating StringBuilder at every loop cycle is a good solution? And is calling a delete instead better, like the following? StringBuilder sb = new StringBuilder(); for (loop condition) { sb.delete(0, sb.length); sb.append("some string"); . . . sb.append(anotherString);

OutOfMemoryError at AbstractStringBuilder enlargeBuffer

让人想犯罪 __ 提交于 2019-11-28 02:50:33
问题 I am reading the youtube response in Stream and converting it to String. using the below code: URL: http://gdata.youtube.com/feeds/api/users/cnn/uploads?&v=2&max-results=25&alt=json private String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); }

Best practices/performance: mixing StringBuilder.append with String.concat

两盒软妹~` 提交于 2019-11-28 02:46:10
I'm trying to understand what the best practice is and why for concatenating string literals and variables for different cases. For instance, if I have code like this StringBuilder sb = new StringBuilder("AAAAAAAAAAAAA") .append(B_String).append("CCCCCCCCCCC").append(D_String) .append("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE") .append("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); Is this the way to do it? From this post , I noticed that the + operator on Strings creates a new instance of StringBuilder, concatenates the operands, and returns a String conversion

Parsing and modifying xml string with sax parser

南笙酒味 提交于 2019-11-28 01:28:08
I have an XML file, I need to search for a specific tag in it and update it's value. The problem is that, using Sax parser is "must". I have to find these tags by using Sax Parser "only", dom stax j4dom dom4j parsers are out of consideration. Can I accomplish this task by converting my xml file to a string and parse it by using sax parser and append the new value by StringBuilder object? Would it be okay? Or what would you recommend? This is a working code, just add missing imports. It uses SAX and changes <name>user1</name> to <name>user2</name> . If you figure out how it works plus read SAX

C# Stringbuilder OutOfMemoryException

蹲街弑〆低调 提交于 2019-11-28 00:42:05
I have written following function public void TestSB() { string str = "The quick brown fox jumps over the lazy dog."; StringBuilder sb = new StringBuilder(); int j = 0; int len = 0; try { for (int i = 0; i < (10000000 * 2); i++) { j = i; len = sb.Length; sb.Append(str); } Console.WriteLine("Success ::" + sb.Length.ToString()); } catch (Exception ex) { Console.WriteLine( ex.Message + " :: " + j.ToString() + " :: " + len.ToString()); } } Now I suppose, that stringbuilder has the capacity to take over 2 billion character (2,147,483,647 to be precise). But when i ran the above function it gave