Testing out someone elses code, I noticed a few JSP pages printing funky non-ASCII characters. Taking a dip into the source I found this tidbit:
// remove any periods from first name e.g. Mr. John --> Mr John
firstName = firstName.trim().replace('.','\0');
Does replacing a character in a String with a null character even work in Java? I know that '\0'
will terminate a C-string. Would this be the culprit to the funky characters?
Should be probably changed to
firstName = firstName.trim().replaceAll("\\.", "");
Does replacing a character in a String with a null character even work in Java?
No.
Would this be the culprit to the funky characters?
Quite likely.
This does cause "funky characters":
System.out.println( "Mr. Foo".trim().replace('.','\0'));
produces:
Mr[] Foo
in my Eclipse console, where the [] is shown as a square box. As others have posted, use String.replace()
.
I think it should be the case. To erase the character, you should use replace(".", "")
instead.
Source: Stackoverflow.com