stringbuilder

Advanced formatting rules for StringBuilder.AppendFormat

こ雲淡風輕ζ 提交于 2020-01-01 05:54:30
问题 I've seen on this site a StringBuilder code sample illustrating AppendFormat usage: using System; using System.Text; class Program { static int[] _v = new int[] { 1, 4, 6 }; static void Main() { StringBuilder b = new StringBuilder(); foreach (int v in _v) { b.AppendFormat("int: {0:0.0}{1}", v, Environment.NewLine); } Console.WriteLine(b.ToString()); } } === Output of the program === int: 1.0 int: 4.0 int: 6.0 Where can I find documentation on those advanced rules for string formatting? 回答1:

Replace text in StringBuilder via regex

橙三吉。 提交于 2019-12-30 21:34:07
问题 I would like to replace some texts in StringBuilder. How to do this? In this code I got java.lang.StringIndexOutOfBoundsException at line with matcher.find() : StringBuilder sb = new StringBuilder(input); Pattern pattern = Pattern.compile(str_pattern); Matcher matcher = pattern.matcher(sb); while (matcher.find()) sb.replace(matcher.start(), matcher.end(), "x"); 回答1: Lets have a StringBuilder w/ 50 total length and you change the first 20chars to 'x'. So the StringBuilder is shrunk by 19,

Writing html in a string

久未见 提交于 2019-12-30 18:27:21
问题 I'm trying a write a couple small lines of html in my java class that gets some data from another API. I get the data in a JSON string, and would then like to display some of it on a webpage. To create the HTML, I try: StringBuilder sb = new StringBuilder(); for(int i=0;i<leads.size();i++){ sb.append("<p>Name: "+leads.get(i).getFirstName()+" "+leads.get(i).getLastName()+"</p>"); sb.append("<p>Email: "+leads.get(i).getEmail()+"</p>"); sb.append("<br />"); } fullLeadData = sb.toString(); But

Java字符串的String、StringBuilder、StringBuffer三者特性详解

戏子无情 提交于 2019-12-30 11:22:04
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 一、不可变String类型 字符串是计算机程序设计中的,最常见行为,Java的字符串操作最主要的类是String,并且String对象是不可变的(Immutable),即对象一旦创建在内存中,那么它的内容就不再改变。虽然String类中提供很多方法看起来像是可以修改String对象,比如trim()、subString()等等,但是实际上它们并没有改变原来的字符串对象,这些方法传递的只是引用的一个拷贝,所以重新创建了一个String类型的对象,并且有了新的引用。 例如下面一段代码可以说明String的不可变特性: package date0804.demo2; public class ImmutableString { public static void main(String[] args){ String str=new String("xyz"); change(str); System.out.println(str); } public static void change(String s) { s="xml"; } } 其输出结果为 str=xyz 因此,我么可以看到每当把String对象作为方法的参数时,都会复制一份引用,而该引用所指的对象其实一直待在单一的物理位置上,从未改变过。 另外

System.Text.StringBuilder 类

淺唱寂寞╮ 提交于 2019-12-30 11:21:51
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 要修改字符串而不创建新的对象,可以使用 System.Text.StringBuilder 类。 StringBuilder.Append 将信息追加到当前 StringBuilder 的结尾。 StringBuilder.AppendFormat 用带格式文本替换字符串中传递的格式说明符。 StringBuilder.Insert 将字符串或对象插入到当前 StringBuilder 对象的指定索引处。 StringBuilder.Remove 从当前 StringBuilder 对象中移除指定数量的字符。 StringBuilder.Replace 替换指定索引处的指定字符。 来源: oschina 链接: https://my.oschina.net/u/2720507/blog/709709

字符串:StringBuilder()

*爱你&永不变心* 提交于 2019-12-30 11:18:16
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> String 字符串常量 StringBuffer 字符串变量(线程安全) StringBuilder 字符串变量(非线程安全) 【1】String和StringBuffer String 类型和 StringBuffer 类型的主要性能区别其实在于 String 是不可变的对象 , 因此在每次对 String 类型进行改变的时候其实都等同于生成了一个新的 String 对象,然后将指针指向新的 String 对象,所以 经常改变内容的字符串最好不要用 String ,因为每次生成对象都会对系统性能产生影响 ,特别当内存中无引用对象多了以后, JVM 的 GC 就会开始工作,那速度是一定会相当慢的。 如果是使用 StringBuffer 类则结果就不一样了, 每次结果都会对 StringBuffer 对象本身进行操作,而不是生成新的对象,再改变对象引用。 所以在一般情况下我们推荐使用 StringBuffer ,特别是字符串对象经常改变的情况下。 在大部分情况下 StringBuffer > String 【2】StringBuffer和StringBuilde StringBuffer Java.lang.StringBuffer 线程安全的可变字符序列 。一个类似于 String 的字符串缓冲区,但

HashMap/HashTable/String/StringBuffer/StringBuilde

落爺英雄遲暮 提交于 2019-12-30 11:13:59
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 自从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类的区别也在于此

Strings are immutable - that means I should never use += and only StringBuffer?

天涯浪子 提交于 2019-12-30 06:01:47
问题 Strings are immutable, meaning, once they have been created they cannot be changed. So, does this mean that it would take more memory if you append things with += than if you created a StringBuffer and appended text to that? If you use +=, you would create a new 'object' each time that has to be saved in the memory, wouldn't you? 回答1: Yes, you will create a new object each time with +=. That doesn't mean it's always the wrong thing to do, however. It depends whether you want that value as a

String StringBuffer StringBuilder AbstractStringBuilder的关联和源码解析

纵然是瞬间 提交于 2019-12-29 21:35:16
目录 前言 概括 StringBuffer类 StringBuilder类 AbstractStringBuilder 后话 前言 相信很多程序员在使用String、StringBuilder和StringBuffer的时候都知道怎么使用,却并不会去看其原理,这里我就整理一下自己的观看心得,如有不足,多多海涵哈! 在学习这三个类之前先认识一下CharSequence接口和Appendable接口: CharSequence接口 ,出自于JDK1.4,有如下几个常用的方法: int length(); 返回字符序列长度 char charAt(int index); 返回字符序列在指定索引处的字符 CharSequence subSequence(int start, int end); 截取字符序列从索引start到end之间的值。包括start不包括end。例如:长度为5的字符序列“12345”,截图0到3的值为“123”,即真正的返回值为索引start到end-1之间的值。 Appendable接口 ,出自JDK1.5,有如下几个常用方法: Appendable append(CharSequence csq) throws IOException; 拼接字符序列 Appendable append(CharSequence csq, int start, int end)

String Concatenation unsafe in C#, need to use StringBuilder?

♀尐吖头ヾ 提交于 2019-12-29 01:41:08
问题 My question is this: Is string concatenation in C# safe? If string concatenation leads to unexpected errors, and replacing that string concatenation by using StringBuilder causes those errors to disappear, what might that indicate? Background: I am developing a small command line C# application. It takes command line arguments, performs a slightly complicated SQL query, and outputs about 1300 rows of data into a formatted XML file. My initial program would always run fine in debug mode.