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?
The "replace is not a function" error occurs when we call the replace() method on a value that is not of type string . To solve the error, convert the value to a string using the toString() method before calling the replace() method.
The String type provides you with the replace() and replaceAll() methods that allow you to replace all occurrences of a substring in a string and return the new version of the string.
Java String replace() MethodThe replace() method searches a string for a specified character, and returns a new string where the specified character(s) are replaced.
Thank you in advance!!! It might be a trick question; Java strings are immutable so you can't change them "in place".
You did not assign it to test
. Strings are immutable.
test = test.replace("KP", "");
You need to assign it back to test
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With