Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

1 of 2
Nebeski
  • 610
  • 5
  • 15

Java replace not working as expected

Here is the code:

public class Test {
 public static void main(String argv[]) {
 String original="r(1,2,1)=r(1,2,0)+r(1,1,0)r(1,1,0)*r(1,2,0)";
 StringTest strtest=new StringTest();
 strtest.setX(original.replace("r(1,2‚0)", "TEST"));
 System.out.println(original.replace("r(1,2,0)", "TEST"));
 System.out.println(strtest);
 }
}
class StringTest {
 String x=null;
 public StringTest() {}
 public String toString() {
 return x;
 }
 public void setX(String other) {
 x=other;
 }
}

Running this gives output:

r(1,2,1)=TEST+r(1,1,0)r(1,1,0)*TEST
r(1,2,1)=r(1,2,0)+r(1,1,0)r(1,1,0)*r(1,2,0)

Why is x in strtest equal to original string and r(1,2,0) is not replaced and how do I fix it?

Nebeski
  • 610
  • 5
  • 15
lang-java

AltStyle によって変換されたページ (->オリジナル) /