Questions tagged [stringbuilder]

Stringbuilder is a class that provides a convenient and efficient way of working with text data in Java and .NET framework.

Stringbuilder is a class that provides a convenient and efficient way of working with text data. Implementation of the stringbuilder can be found in .NET framework and Java.

The very idea of the class is being a character array with the capability to insert and append new characters. It avoids copying the strings in the memory, hence improves the performance.

2114 questions
13
votes
6 answers

Appending with StringBuilder

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…
Froyo
  • 455
  • 1
  • 8
  • 21
13
votes
4 answers

Is it required to check before replacing a string in StringBuilder (using functions like "Contains" or "IndexOf")?

Is there any method IndexOf or Contains in C#. Below is the code: var sb = new StringBuilder(mystring); sb.Replace("abc", "a"); string dateFormatString = sb.ToString(); if (sb.ToString().Contains("def")) { sb.Replace("def", "aa"); } if…
Rocky Singh
  • 15,128
  • 29
  • 99
  • 146
13
votes
3 answers

StringBuilder insert() vs append() performance?

Is there any difference in the performance of insert() vs append() from StringBuilder class? I will be building plenty of short string as text identifiers and asked myself this question... Should I initialize SB with a separator and use insert +…
Palcente
  • 625
  • 2
  • 7
  • 21
13
votes
9 answers

Pass-by-value (StringBuilder vs String)

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…
Helenesh
  • 3,999
  • 2
  • 21
  • 36
13
votes
3 answers

Is StringBuilder threadsafe (using it with parallelStream)?

My code: StringBuilder sb = new StringBuilder(); events.parallelStream().forEach(event -> { sb.append(event.toString()); sb.append("\n"); }); I don't care about the order of the events.toString() in the final result. But I care that the…
seinecle
  • 10,118
  • 14
  • 61
  • 120
13
votes
4 answers

How to convert byte array to hex format in Java

I know that you can use printf and also use StringBuilder.append(String.format("%x", byte)) to convert values to HEX values and display them on the console. But I want to be able to actually format the byte array so that each byte is displayed as…
James Meade
  • 1,147
  • 5
  • 22
  • 46
13
votes
1 answer

OutOfMemoryError at AbstractStringBuilder enlargeBuffer

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) { …
wolverine
  • 1,665
  • 5
  • 24
  • 43
13
votes
5 answers

Use StringBuilder to pad String with blank spaces or other characters

I'm a beginner to java and this is my first post on Stackoverflow. Although my initial code is similar to other posts here, my question relates to implementing StringBuilder and so the reason for this post. I've created a method **Initial…
user1586954
  • 141
  • 2
  • 2
  • 4
12
votes
7 answers

Why is StringBuilder slower than string concatenation?

Why is StringBuilder slower when compared to + concatenation? StringBuilder was meant to avoid extra object creation, but why does it penalize performance? static void Main(string[] args) { int max = 1000000; for (int times…
Junior Mayhé
  • 16,144
  • 26
  • 115
  • 161
12
votes
2 answers

Append Whitespace to stringbuilder?

How do I append white space to a string builder? I need to insert 90 blank spaces, in VB i had this code but i was confused how to write in c# can any one help me Dim S As New StringBuilder("HELLO") S.Append(" "c,…
Developer
  • 8,390
  • 41
  • 129
  • 238
12
votes
3 answers

Multiple simultaneous substring replacements in Java

(I come from the python world, so I apologise if some of the terminology I use jars with the norm.) I have a String with a List of start/end indices to replace. Without getting too much into detail, consider this basic mockup: String text = "my…
cs95
  • 379,657
  • 97
  • 704
  • 746
12
votes
1 answer

How is it possible that StringBuilder.setLength(0) invokes Arrays.fill?

While investigating a performance test result I found the following stack trace reported in Java Flight Recorder's "Hot Methods": Stack Trace Sample Count Percentage(%) ----------- …
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
12
votes
2 answers

How much does Java optimize string concatenation with +?

I know that in more recent Java versions string concatenation String test = one + "two"+ three; Will get optimized to use a StringBuilder. However will a new StringBuilder be generated each time it hits this line or will a single Thread Local…
Tim B
  • 40,716
  • 16
  • 83
  • 128
12
votes
5 answers

Creating safe SQL statements as strings

I'm using C# and .NET 3.5. I need to generate and store some T-SQL insert statements which will be executed later on a remote server. For example, I have an array of Employees: new Employee[] { new Employee { ID = 5, Name = "Frank Grimes" }, …
Jason Anderson
  • 9,003
  • 5
  • 30
  • 25
12
votes
3 answers

Why is StringBuilder much faster than String?

Why is StringBuilder much faster than string concatenation using the + operator? Even though that the + operator internally is implemented using either StringBuffer or StringBuilder. public void shortConcatenation(){ long startTime =…
Jason
  • 366
  • 2
  • 4
  • 17