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]); }
Related
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
This question already has answers here:
Java String Concatenation
(7 answers)
Closed 6 years ago.
I want to print both string and integer at once in text field.
Just use string concatenation:
tf3.setText(str + " " + int1);
instead of two tf3.settext();
do
tf3.setText(str + int1)
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.
This question already has answers here:
Using variables to store values or directly get values from the objects?
(3 answers)
Closed 7 years ago.
Is there a difference between this:
String str1 = "abcabc";
String str2 = str1.replaceAll("a", "");
System.out.print(str2);
And
String str1 = "abcabc";
System.out.print(str1.replaceAll("a", ""));
In terms of memory used, or in other words, will the print method create memory in the heap for the new string?
str1.replaceAll("a", "")
returns a new String instance. So "Yes", it does create it on the heap, & "Yes" they are the same. (memory speaking)
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.