Netto
Netto

Reputation: 284

Java replace/replaceAll strange behavior

I can't get what I'm missing here. Both replace and replaceAll from java.lang.String are generating a question mark (?) after each ocurrence:

        String str = "ABCD DKABCED DLS ABC";        
        System.out.println("str='"+str+"'");
        System.out.println("str.replaceAll(\"ABC\", \"A\\\\${BC}​\" ) => " + str.replaceAll("ABC", "A\\${BC}​" ));
        System.out.println("str.replace(\"ABC\", \"A${BC}​\" ) => " + str.replace("ABC", "A${BC}​" ));

Generates the following output:

str='ABCD DKABCED DLS ABC'
str.replaceAll("ABC", "A\\${BC}?" ) => A${BC}?D DKA${BC}?ED DLS A${BC}?
str.replace("ABC", "A${BC}?" ) => A${BC}?D DKA${BC}?ED DLS A${BC}?

Here an image of the execution: enter image description here

Does anybody knows why?

EDITED:

Just for the record. The problem it that there really WAS a character after the brackets. After coping and pasting to Notepad++ I could see the }?"text. Not in Netbeans. So purelly enconding missunderstanding.

Upvotes: 2

Views: 731

Answers (2)

DNA
DNA

Reputation: 42597

I suspect this is a character encoding problem. When I pasted your code into Eclipse (on Windows) it could not save the code, complaining about the character set:

Some characters cannot be mapped using "Cp1252" character encoding.

When I retyped it in from scratch, the problem went away:

String str = "ABCD DKABCED DLS ABC";
System.out.println("str='" + str + "'");
System.out.println(str.replace("ABC", "A${BC}"));

produces the following (without extra ? marks):

str='ABCD DKABCED DLS ABC'
A${BC}D DKA${BC}ED DLS A${BC}

If you take the hexdump of a normal } you get 7d.

But for the } character in your code, I get 7d e2 80 8b

Upvotes: 2

HavelTheGreat
HavelTheGreat

Reputation: 3386

That would be because you have question marks in your replacement string. Thus replace and replaceAll are simply doing exactly what you are telling them to do.

Upvotes: 0

Related Questions