This question already has answers here:
Java String replace not working [duplicate]
(6 answers)
Closed 9 years ago.
I am a little bit confused at the moment. I tried that:
String test = "KP 175.105";
test.replace("KP", "");
System.out.println(test);
and got:
KP 175.105
However, I want:
175.105
What's wrong with my code?
You did not assign it to test. Strings are immutable.
test = test.replace("KP", "");
You need to assign it back to test.
Strings are immutable so you need to assign your test reference to the result of String.replace:
test = test.replace("KP", "");
String is immutable in java so you have to do
test =test.replace("KP", "");
Related
This question already has answers here:
String variable interpolation Java [duplicate]
(5 answers)
Closed 1 year ago.
Is there any way to join a string and another variable together, that is also a string, without using the "+" operator. Specifically one similar to this one that is done in C#.
string str = $"Hello {userName}. Today is {date}.";
If there is any way to achieve a similar or same outcome in Java please let me know.
See https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
String.format("Hello %s. Today is %s.", userName, date);
This question already has answers here:
Run piece of code contained in a String
(9 answers)
Closed 5 years ago.
I'm trying to convert a string in code in Java, but i have no idea how to do it or if it is possible.
This is my Java code (Measure is an other class I have created)
String str= "Measure m = new Measure(10,1);";
Is it possible to run the code in the string?
No I dont think that is a good practice, you don't need to do it that way,
just instantiate outside of the string, it will be fine and good practice.
Measure m = new Measure(10,1);
This question already has answers here:
What is the difference between "text" and new String("text")?
(13 answers)
Closed 7 years ago.
String is an Object. Why it is possible to initialize it the same way as primitive type: String str = "my string";
I was expecting to see initialization by using constructor only: new String("my string");
This is just a simplification provided by java. The other alternative would be enormous ugly. Your alternative solution has one simple logical mistake:
new String("my string");
Just aswell uses a string-literal as simply "my string". The real alternative would be
new String(new char[]{'m','y',' ',...,'n','g'});
Or just the same example using a byte[] (deprecated), which would look even worse.
You can go to the javadocs:
Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Why does the toString method in java not seem to work for an array
(9 answers)
Closed 9 years ago.
When i try to split a String "Element" into a String array, and then print out the result i get an output which i don't understand. My code is as follows:
String element = mapArray.get(i);
elementSplit = element.split("(?!^)");
System.out.println(elementSplit);
And the output produced when i print the String Array is:
[Ljava.lang.String;#3dee2310
Could someone please advise, as i do not know why it is printing this output.
Thanks very much
You have to use Arrays.toString method.
System.out.println(Arrays.toString(elementSplit));
Due to speed You should use toCharArray method instead of split("(?!^)") and in order to print array you should use Arrays.toString method
String element = mapArray.get(i);
elementSplit = element.toCharArray();
System.out.println(Arrays.toString(elementSplit));
This question already has answers here:
Java String replace not working [duplicate]
(6 answers)
Closed 9 years ago.
I am a little bit confused at the moment. I tried that:
String test = "KP 175.105";
test.replace("KP", "");
System.out.println(test);
and got:
KP 175.105
However, I want:
175.105
What's wrong with my code?
You did not assign it to test. Strings are immutable.
test = test.replace("KP", "");
You need to assign it back to test.
Strings are immutable so you need to assign your test reference to the result of String.replace:
test = test.replace("KP", "");
String is immutable in java so you have to do
test =test.replace("KP", "");