stringbuilder

What actual cause the StringBuilder fails in multi threading environment

流过昼夜 提交于 2019-12-24 05:13:08
问题 StringBuffer is synchronized but StringBuilder is not ! This has been discussed deeply at Difference between StringBuilder and StringBuffer. There is an example code there (Answered by @NicolasZozol), which address two issues: compares the performance of these StringBuffer and StringBuilder shows the StringBuilder could fail in a multithread environment. My question is about second part , exactly what makes it to go wrong?! When you run the code some times, the stack trace is displayed as

Java compiler doesn't optimize String concatenation automatically?

孤街醉人 提交于 2019-12-23 17:26:47
问题 The following Jsoup code concatenates the text of all elements in container els : for (Element el : els) entireText += el.text(); On a container with ~64 elements, each containing ~1KB (totaling in entireText being ~64KB), this simple loop takes about 8 seconds on a typical low-end Android phone. This slow performance kind of surprises me because I was under the impression that Java compilers substitute expressions like A + B + C with new StringBuilder(A).append(B).append(C).toString() . Is

Does StringBuilder.toString retain the built string?

走远了吗. 提交于 2019-12-23 12:21:10
问题 I'm creating a StringBuilder to collect strings that I periodically flush to a server. If the flush fails, I want to keep the strings to try again next time, although in the mean time I might get additional strings to send which must be added to the StringBuilder. What I want to know is what the most efficient way to do this would be, as this is being done in an Android app where battery usage and thus CPU usage is a big concern. Does calling StringBuilder's toString() function store the

Add byte order mark to a string via StringBuilder

℡╲_俬逩灬. 提交于 2019-12-23 11:56:50
问题 How can I add a byte order mark to a StringBuilder? (I have to pass a string to another method which will save it as a file, but I can't modify that method). I tried this: var sb = new StringBuilder(); sb.Append('\xEF'); sb.Append('\xBB'); sb.Append('\xBF'); But when I view it with hex editor, it adds the following sequence: C3 AF C2 BB C2 BF The string is huge, so it would be good to do it without back and forth converting to byte array. Edit: Clarification after questions in comments. I

java个人学习笔记-14(Java StringBuffer 和 StringBuilder 类)

限于喜欢 提交于 2019-12-23 11:19:12
Java StringBuffer Java StringBuffer 和 StringBuilder 类 Java StringBuffer 和 StringBuilder 类 当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。 和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。 StringBuilder 类在 Java 5 中被提出,它和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的(不能同步访问)。 由于 StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类。然而在应用程序要求线程安全的情况下,则必须使用 StringBuffer 类。 例子: public class Aa { public static void main ( String args [ ] ) { StringBuilder sBuilder = new StringBuilder ( "hello " ) ; sBuilder . append ( "java" ) ; sBuilder . append ( " !!!" ) ;

Using HtmlTextWriter to Render Server Controls?

青春壹個敷衍的年華 提交于 2019-12-23 10:22:36
问题 I'm writing the RenderContents() method of my ASP.NET server control. The method uses an HtmlTextWriter object to render the output content. For the control I'm writing, using the HtmlTextWriter 's methods seems like it will require a lot of lines of code to open and close every tag and add every attribute to the stream. In the end I feel like I'm going to end up with code that is a lot longer than it needs to be. I was thinking that if I used a chainable class such as StringBuilder , my code

string.replace vs StringBuilder.replace for memory [duplicate]

删除回忆录丶 提交于 2019-12-23 08:29:21
问题 This question already has answers here : String.Replace() vs. StringBuilder.Replace() (9 answers) Closed 6 years ago . I have downloaded a stream as a byte[] 'raw' that is about 36MB. I then convert that into a string with string temp = System.Text.Encoding.UTF8.GetString(raw) Then I need to replace all "\n" with "\r\n" so I tried string temp2 = temp.Replace("\n","\r\n") but it threw an "Out of Memory" exception. I then tried to create a new string with a StringBuilder: string temp2 = new

JAVA字符串

纵然是瞬间 提交于 2019-12-23 08:23:46
JAVA 字符串 java 提供了 String 类来创建和操作字符串。但是,String类不可变类,即一旦一个string 对象被创建后,包含在这个对象之中的字符序列时不可改变的,直至这个对象被销毁。 java 同时提供了两个可变字符串类 StringBuffer 和 StringBuildeer,两者功能基本相似,方法野差不多。不同的是,StringBufeffer 是线程安全的,而StringBuilder 则是没有实现线程安全功能,所以性能略高。 String类 直接定义字符串: 直接定义字符串是指使用双引号表示字符串中的内容,具体方法是用字符串常量直接初始化一个String对象,如下: String str = "hello,world" 使用String类定义 String()初始化一个新创建的String对象,表示一个字符序列。 String(String original)初始化一个新创建的String对象,使其表示一个与参数相同的字符序列。 String(char [] value)分配一个新的字符串,将参数中的字符数组元素全部变为字符串。 StringBuilder类 java提供了两个可变字符串类StringBuffer 和StringBuilder。创建StringBuilder类的对象后可以随意修改字符串的内容

一个简单的ORM制作(CURD操作类)

亡梦爱人 提交于 2019-12-23 02:02:36
SQL执行类 CURD操作类 其他酱油类 此篇是为上篇文章填坑的,不知道上篇砸过来的砖头够不够,不够的话请大家继续砸。 CURD操作类负责将用户提供的条件转换为SQL语句,并提供给IHelper执行,返回Model集合. CURD类需要一个接口抽象出公共方法.便于修改和扩展,提供泛型接口。为了简单起见暂时未提供JOIN的实现,可以以数据库视图替代 public interface IDbOper<T> : IDisposable where T : new() { object Insert(T m);//新增MODEL,返回ID,简单起见只做了INT自增 int Update(string str);//批量更新 int Update(T m);//Model更新 int Delete();//删除 ///拼接字符版,需要自己防止注入,特别是Orderby容易被忽视 IDbOper<T> Select(string sl);//选择字段 IDbOper<T> Where(string sl); IDbOper<T> Orderby(string orby); ///Expression版重载,转化为参数方式执行,以参数方式拼接无注入风险 IDbOper<T> Select(Expression<Func<T, object>> sl); IDbOper<T> Where

When should we change a String to a Stringbuilder?

好久不见. 提交于 2019-12-22 10:48:35
问题 In an application a String is a often used data type. What we know, is that the mutation of a String uses lots of memory. So what we can do is to use a StringBuilder/StringBuffer. But at what point should we change to StringBuilder? And what should we do, when we have to split it or to remplace characters in there? eg: //original: String[] split = string.split("?"); //better? : String[] split = stringBuilder.toString().split("?); or //original: String replacedString = string.replace("l","st")