stringbuilder

Fastest way to pad a number in Java to a certain number of digits

强颜欢笑 提交于 2020-01-04 08:10:26
问题 Am trying to create a well-optimised bit of code to create number of X-digits in length (where X is read from a runtime properties file), based on a DB-generated sequence number (Y), which is then used a folder-name when saving a file. I've come up with three ideas so far, the fastest of which is the last one, but I'd appreciate any advice people may have on this... 1) Instantiate a StringBuilder with initial capacity X. Append Y. While length < X, insert a zero at pos zero. 2) Instantiate a

Refactor String concatenation to StringBuilder with intelliJ

倾然丶 夕夏残阳落幕 提交于 2020-01-04 05:36:35
问题 I was designated to make refactoring on a project, and I came across this situation this.path = DESTINY + deploy.name() + FILE_SEPARATOR + delivery.getSystem().getCode() + FILE_SEPARATOR + delivery.getName() + FILE_SEPARATOR + delivery.getEnviroment().getName(); I want to refactor it to something like this: StringBuilder tmpPath = new StringBuilder(); tmpPath.append(DESTINY); tmpPath.append(deploy.name()); tmpPath.append(FILE_SEPARATOR); tmpPath.append(delivery.getSystem().getCode()); tmpPath

How to make a part of Stringbuilder content bold?

三世轮回 提交于 2020-01-04 02:12:10
问题 StringBuilder sb = new StringBuilder(); sb.Append( string.Format("{0} |{1} ", Name, Value) ); Display.Text = sb.ToString(); // Display is a WP7 TextBlock control I want to make "Name" as bold. Is it possible to do that ? 回答1: You'll need to put the text into a RichTextBox and have the name as a separate Run in the Paragraph as in this example from the MSDN: // Create a Run of plain text and some bold text. Run myRun1 = new Run(); myRun1.Text = "A RichTextBox with "; Bold myBold = new Bold();

Java string concat in stringbuilder call

烂漫一生 提交于 2020-01-03 13:09:09
问题 As far as I know, StringBuilder helps to reduce memory usage by not creating temporary string instances in the string pool during concats. But, what happens if I do sth like this: StringBuilder sb = new StringBuilder("bu"); sb.append("b"+"u"); Does it compile into sb.append("b"); sb.append("u"); ? Or it depends on optimalization flags? Or I loose the whole benefit if stringbuilders? Or this quetion makes no sense? :) 回答1: It compiles to sb.append("bu") , because the compiler translates the

Java string concat in stringbuilder call

◇◆丶佛笑我妖孽 提交于 2020-01-03 13:08:52
问题 As far as I know, StringBuilder helps to reduce memory usage by not creating temporary string instances in the string pool during concats. But, what happens if I do sth like this: StringBuilder sb = new StringBuilder("bu"); sb.append("b"+"u"); Does it compile into sb.append("b"); sb.append("u"); ? Or it depends on optimalization flags? Or I loose the whole benefit if stringbuilders? Or this quetion makes no sense? :) 回答1: It compiles to sb.append("bu") , because the compiler translates the

String Vs Stringbuffer as HashMap key

守給你的承諾、 提交于 2020-01-02 07:05:35
问题 I am trying to understand why String and Stringbuilder/StringBuffer are treated differently when used as Hashmap keys. Let me make my confusion clearer with the following illustrations: Example #1, using String: String s1 = new String("abc"); String s2 = new String("abc"); HashMap hm = new HashMap(); hm.put(s1, 1); hm.put(s2, 2); System.out.println(hm.size()); Above code snippet prints '1'. Example #2, using StringBuilder(or StringBuffer): StringBuilder sb1 = new StringBuilder("abc");

How to print upto two decimal places in java using string builder?

若如初见. 提交于 2020-01-02 02:19:33
问题 hi i am trying to print after dividing in string builder and printing that string builder let show me my code , string.append("Memomry usage:total:"+totalMemory/1024/1024+ "Mb-used:"+usageMemory/1024/1024+ " Mb("+Percentage+"%)-free:"+freeMemory/1024/1024+ " Mb("+Percentagefree+"%)"); in above code "totalmemory" and "freememory" is of double type having bytes value in point not null so i divide it by "1024" two times to get it in "Mb" and "string" is variable of string builder after using

Pass-by-value (StringBuilder vs String) [duplicate]

◇◆丶佛笑我妖孽 提交于 2020-01-02 00:58:12
问题 This question already has answers here : Is Java “pass-by-reference” or “pass-by-value”? (86 answers) Closed 4 years ago . I do not understand why System.out.println(name) outputs Sam without being affected by the method's concat function, while System.out.println(names) outputs Sam4 as a result of the method's append method. Why is StringBuilder affected and not String? Normally, calling methods on a reference to an object affects the caller, so I do not understand why the String result

c#通用json帮助类

女生的网名这么多〃 提交于 2020-01-01 20:02:19
常用json方法,转换函数, using System; using System.Data; using System.Text; using System.Collections.Generic; using System.Reflection; using System.Data.Common; using System.Collections; using System.IO; using System.Text.RegularExpressions; using System.Runtime.Serialization.Json; namespace ZZUdp.Tool { public static class JsonHepler { /// <summary> /// List转成json /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonName"></param> /// <param name="list"></param> /// <returns></returns> public static string ListToJson<T>(IList<T> list, string jsonName) { StringBuilder Json = new

学JAVA第二十四天,Set集合与StringBuilder

瘦欲@ 提交于 2020-01-01 15:53:04
下面的内容需要慢慢看,因为,我的语言表达能力不是很好 首先说Set把,Set集合是一个无序且不允许重复的集合,而且查找效率也是快的可怕的。 但是,有些时候,我们必须要用储存多个相同的值时,Set也是可以通过特殊的方法来储存 相同的值的。 例如,我们创一个实体类,通过实体类来实现。 这样: 创建实体类: public class Strings{ private String a; public void A(String a){ this.a=a; } public String getA(){ return this.a; }   @Override public int hashCode(){ return a.hashCode();//可以先运行一遍,然后再把这个方法注释再运行一遍 }              //对比一下效果 @Override public boolean equals(Object obj){//上一个对比后,然后,又把这个方法注释运行一遍 return true;            //对比一下效果 } public String toString(){ return this.a; } } 然后,测试类: import java.util.*; public class Test{ public static void main(String