stringbuilder

C# or Java: Prepend strings with StringBuilder?

旧时模样 提交于 2019-11-30 11:11:05
问题 I know we can append strings using StringBuilder . Is there a way we can prepend strings (i.e. add strings in front of a string) using StringBuilder so we can keep the performance benefits that StringBuilder offers? 回答1: Using the insert method with the position parameter set to 0 would be the same as prepending (i.e. inserting at the beginning). An example is: varStringBuilder.insert(0, "someThing"); It works both for C# and Java 回答2: Prepending a String will usually require copying

Newline character in stringbuilder

走远了吗. 提交于 2019-11-30 07:47:29
How do you append new line(\n\r) character in StringBuilder ? I would make use of Environment.NewLine Property Something like StringBuilder sb = new StringBuilder(); sb.AppendFormat("Foo{0}Bar", Environment.NewLine); string s = sb.ToString(); Or StringBuilder sb = new StringBuilder(); sb.Append("Foo"); sb.Append("Foo2"); sb.Append(Environment.NewLine); sb.Append("Bar"); string s = sb.ToString(); EDIT: If you wish to have a new line after each append, you can have a look at @Ben Voigt answer. with the AppendLine method also, using StringBuilder.AppendLine method. Kerry Jiang It will append \n

【读阿里规约有感】循环体内的字符串连接时,使用StringBuilder的append()和+String的方式比较

倖福魔咒の 提交于 2019-11-30 07:34:45
阿里规约里原话是: 【推荐】循环体内,字符串的连接方式,请使用 StringBuilder 的 append 方法进行扩展。(而不要用+String的方式) 说明:因为反编译出的字节码文件显示每次循环都会 new 出一个 StringBuilder 对象,然后进行 append 操作,最后通过 toString 方法返回 String 对象,造成内存资源浪费。 从这句话得知,用StringBuilder连接字符串比 + 的方式好,因为更少占用内存资源。可是我们怎么才能更加直观的看到性能对比呢? 这时我想到了时间开销对比,我推测前者的运行时间比后者少,于是有了下面的测试代码 @Testpublic void mainTest1(){ long startTime1 = System.currentTimeMillis(); StringBuilder str1 = new StringBuilder("start"); for (int i = 0;i < 100000; i++){ str1.append(i); } long endTime1 = System.currentTimeMillis(); long time1 = endTime1 - startTime1; System.out.println("StringBuilder所花时间:" + time1);

LINQ to append to a StringBuilder from a String[]

守給你的承諾、 提交于 2019-11-30 07:33:39
问题 I've got a String array that I'm wanting to add to a string builder by way of LINQ. What I'm basically trying to say is "For each item in this array, append a line to this StringBuilder". I can do this quite easily using a foreach loop however the following code doesn't seem to do anything. What am I missing? stringArray.Select(x => stringBuilder.AppendLine(x)); Where as this works: foreach(String item in stringArray) { stringBuilder.AppendLine(item); } 回答1: If you insist on doing it in a

Appending with StringBuilder

帅比萌擦擦* 提交于 2019-11-30 07:27:34
问题 I have 5 checkboxes and I have to select multiple checkboxes. I made a code like this to check check box is checked: sports=(CheckBox)findViewById(R.id.sports_btn); sports.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (sports.isChecked() == true) list.add("4"); } }); I am adding a value to an array list: list ArrayList<String> list = new ArrayList<String>(); and I am retrieving the value as string like this: StringBuilder sb = new StringBuilder(); for

Java学习资料-StringBuilder与StringBuffer的区别

荒凉一梦 提交于 2019-11-30 07:11:42
相信大家看到过很多比较String和StringBuffer区别的文章,也明白这两者的区别,然而自从Java 5.0发布以后,我们的比较列表上将多出一个对象了,这就是StringBuilder类。String类是不可变类,任何对String的改变都会引发新的String对象的生成;而StringBuffer则是可变类,任何对它所指代的字符串的改变都不会产生新的对象,可变和不可变类这一对对象已经齐全了,那么为什么还要引入新的StringBuilder类干吗?相信大家都有此疑问,我也如此。下面,我们就来看看引入该类的原因。 为什么会出现那么多比较String和StringBuffer的文章? 原因在于当改变字符串内容时,采用StringBuffer能获得更好的性能。既然是为了获得更好的性能,那么采用StringBuffer能够获得最好的性能吗? 答案是NO! 为什么? 如果你读过《Think in Java》,而且对里面描述HashTable和HashMap区别的那部分章节比较熟悉的话,你一定也明白了原因所在。对,就是支持线程同步保证线程安全而导致性能下降的问题。HashTable是线程安全的,很多方法都是synchronized方法,而HashMap不是线程安全的,但其在单线程程序中的性能比HashTable要高。StringBuffer和StringBuilder类的区别也在于此

Has anyone implemented a Regex and/or Xml parser around StringBuilders or Streams?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 07:10:59
问题 I'm building a stress-testing client that hammers servers and analyzes responses using as many threads as the client can muster. I'm constantly finding myself throttled by garbage collection (and/or lack thereof), and in most cases, it comes down to strings that I'm instantiating only to pass them off to a Regex or an Xml parsing routine. If you decompile the Regex class, you'll see that internally , it uses StringBuilders to do nearly everything, but you can't pass it a string builder; it

String,StringBuilder,StringBuffer

为君一笑 提交于 2019-11-30 06:28:22
String:字符串类,由多个字符组成的一串数字,字符串其本质是一个字符串数组 利用构造对象创建的String对象与直接赋值的区别:   通过构造方法字符串对象是在堆内存   直接赋值创建的对象是在方法区的常量池    来源: https://www.cnblogs.com/dajingshao/p/11568483.html

从0基础开始学习Java第6天

此生再无相见时 提交于 2019-11-30 05:49:05
今天头痛,有可能感冒了,学的少了一点,但是应该坚持的学,一旦间断,有可能就停下啦。加油! 1、自动装箱、自动拆箱 这两个机制是为了减小程序员的压力吧。 有时候需要定义数值型的对象,以Int为例,可以用一个叫做Integer的类声明数值型的对象。感觉也比较简单,要理解的东西都放在注释里了。 public class TestAtuoBoxing { public static void main(String[] args) { Integer a=234;//Integer a=Integer.valueof(234)自动装箱 int b=a;//编译器会修改成int b=a.intValue();自动拆箱 Integer c=null; //int d=c;//自动拆箱,调用了c.intValue();空指针 if (c!=null) {//加判断防止异常 int d=c; } //缓存[-128,127]之间的数字,实际就是系统初始的时候创建了[-128,127]之间的一个缓存数组 //当我们调用valueof()的时候,首先检查时候在[-128,127]之间,如果在这个范围则直接从缓存数组中拿出来已经缓存数组中拿出已经建好的对象 //如果不在这个范围,则创建新的Integer对象 Integer in1=-128; Integer in2=-128; System.out

Java CharAt() and deleteCharAt() performance

爱⌒轻易说出口 提交于 2019-11-30 04:46:43
I've been wondering about the implementation of charAt function for String/StringBuilder/StringBuffer in java what is the coomplexity of that ? also what about the deleteCharAt() in StringBuffer/StringBuilder ? For String , StringBuffer , and StringBuilder , charAt() is a constant-time operation. For StringBuffer and StringBuilder , deleteCharAt() is a linear-time operation. StringBuffer and StringBuilder have very similar performance characteristics. The primary difference is that the former is synchronized (so is thread-safe) while the latter is not. Let us just look at the corresponding