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:
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", "");
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:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I'm trying to do a comparison in Java with 2 strings containing a extended ASCII character.
boolean result = "éasdfasdf".substring(0,1).equals("é");
Can somebody explain why this results false? I think it has something to do with character encoding, but I can't figure out what exactly the problem is here...
Update: ideone.com does successfully run these 2 lines, so the problem is locally in my box. I think I found some more proof of that:
System.out.println("éb".charAt(1) == 'b');
Does also fails... Can it be the problem of 2 different character encodings?
Use
boolean result = "éasdfasdf".substring(0,1).equals("é")
And it will give expected result!The reason is simple - using '==' you compare objects by reference, not by value. So equals() solves this problem
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));