Best way of concatenate String in java [duplicate] - java

This question already has answers here:
String concatenation: concat() vs "+" operator
(12 answers)
Closed 8 years ago.
What is the best way to concatenate two Strings in java. likely best performance and less memory usage. Please help me..!

StringBuilder stringBuilder = new StringBuilder(firstString);
stringBuilder.append(secondString);
String string = stringBuilder.toString();
But according to #a_horse_with_no_name the following is even more efficient
StringBuilder sb = new StringBuilder(firstString.length() + secondString.length());
sb.append(firstString);
sb.append(secondString);
String string = sb.toString();

Related

concatenation and displaying a string [duplicate]

This question already has answers here:
Does concatenating strings in Java always lead to new strings being created in memory?
(3 answers)
Converting String to "Character" array in Java
(14 answers)
String to char array Java
(1 answer)
Closed 3 months ago.
a string created with String class can not be modified.
but when we use the += operator does it mean that the original string change?
exp:
String ch="hello"; ch+= "world";
another question:
why we don't use these instructions to display the string ?
for (int i=0;i<ch.length();i++) {System.out.println(ch[i]); }
i tried this
for (int i=0;i<ch.length();i++) {System.out.println(ch.charAt(i)); }
why it is not similar to
for (int i=0;i<ch.length();i++) {System.out.println(ch[i]); }

How can I Split the String Correctly? [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 3 years ago.
I am getting a result of "kjhvkjkasdfkjh#grouping#group_group.12018-08-20".
I am looking to split the string in java to only get the "2018-08-20".
Any suggestions? The part I want is always at the end.
String sample = "kjhvkjkasdfkjh#grouping#group_group.12018-08-20"
int SubstringStart = sample.length() - 10;
String outputsample = sample.substring(SubstringStart);
will break if string has length shorter than 10

How string concat method works [duplicate]

This question already has answers here:
String is immutable. What exactly is the meaning? [duplicate]
(19 answers)
Closed 6 years ago.
I know String in Java is immutable. Consider:
String s = "Java";
s = s.concat(" is simple");
System.out.println(s); //prints Java is simple
Here the value of String s is changed? Can someone explain me, how String concat actually works?
s.concat(" is simple") returns a new string and s = s.concat(" is simple") assigns this new string to variable s.

Best approach of Creating String Object [duplicate]

This question already has answers here:
Java Strings: "String s = new String("silly");"
(23 answers)
What is the difference between "text" and new String("text")?
(13 answers)
Closed 8 years ago.
In java we can create String in the following 2 ways -
String str1 = new String("first string"); //1
String str2 = "second string"; //2
Is there any difference in performance with these 2 approaches? And in the second case is there any new String object created?
Thanks in advance.
The first approach forces the creation of a new String object. The second allows java to use the constant from the string pool, and should generally be preferred.

When is `StringBuilder` is preferred instead appending `String` to `String`? [duplicate]

This question already has answers here:
StringBuilder vs String concatenation in toString() in Java
(20 answers)
When to use StringBuilder in Java [duplicate]
(9 answers)
Closed 9 years ago.
Below are two ways how to append String:
String firstString = "text_0";
String secondString = "text_1";
String resultString = firstString + secondString;
StringBuilder sb = new StringBuilder();
sb.append(firstString).append(secondString);
String resultString = sb.toString();
My question is - when is more effective to use StringBuilder? Let's say there are 10 strings, and I need to create one of them.
Because StringBuilder can "append" a string instead of concatenating two strings each time creating a new object. Even if you use += operator with Strings a new object is created. This advantage will only become relevant once you try to concatenate a great number of strings. If is also consiedered a bit more readable.
Two main Advantages:
Mutable
Not Synchronized.

Categories