This discussion is archived
7 Replies Latest reply: Dec 3, 2012 5:52 AM by 955926 RSS

String replace method

955926 Newbie
Currently Being Moderated
Hi all

Im writing a simple post ocr correction program and have come across something I dont understand

Look at the method below - simply replace one string with another (works ok mostly but unsure on replacing text with non Letter chars)

public String replaceGroup(String in){          
String word = in;
          
          word = in.replace("\"IT", "TT");// "IT not working
          word = in.replace("'I'l'", "TT");// "I'l' not working
          word = in.replace("'I'I'", "TT");// 'I'I'
          
          return word;
}

Its not a big problem but I curious whats going on here - im assuming this is something to do with escape characters. I cant understand why the last one works but the first 2 dont

Just a bit confused as of why. Im not looking for a way to resolve this as i could just write my own replace method: i just want to know why its not working like this

thanks
Matt
  • 1. Re: String replace method
    EJP Guru
    Currently Being Moderated
    You need four backslashes: two for the Java compiler and two more for the regex. So, "\\\\"IT".
  • 2. Re: String replace method
    955926 Newbie
    Currently Being Moderated
    it wont compile with 4 \: the compiler treats it as esc, \(char), esc, \(char), "(end of string), IT

    thanks for the quick reply
  • 3. Re: String replace method
    EJP Guru
    Currently Being Moderated
    So did you try three? Or are you just waiting here, maybe forever?
  • 4. Re: String replace method
    955926 Newbie
    Currently Being Moderated
    Tried all numbers 1-9 none work, the ones that compile run but dont replace string. Besides that doesnt explain why *'I'I'* isnt being replaced

    Edited by: 952923 on Dec 3, 2012 1:49 AM

    Edited by: 952923 on Dec 3, 2012 1:50 AM
  • 5. Re: String replace method
    gimbal2 Guru
    Currently Being Moderated
    EJP wrote:
    You need four backslashes: two for the Java compiler and two more for the regex. So, "\\\\"IT".
    (regex? Its replace(), not replaceAll())
  • 6. Re: String replace method
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    952923 wrote:
              word = in.replace("\"IT", "TT");// "IT not working
    You should rather state not compilable ...
    http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace(char,%20char)
    vs.
    http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)

    Then your Patterns work great:
      @Test
        public final void testEscapes() {
            Assert.assertEquals("\"IT -> XX", "XX", "\"IT".replaceAll("\"IT", "XX"));
            Assert.assertEquals("I'I' -> XX", "XX", "I'I'".replaceAll("I'I'", "XX"));
            Assert.assertEquals("'I'I' -> XX", "XX", "'I'I'".replaceAll("'I'I'", "XX"));
        }{code}
    
    bye
    TPD                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
  • 7. Re: String replace method
    955926 Newbie
    Currently Being Moderated
    Cheers thats what i was looking for. I never have much luck with the replace method.

    Thanks
    Matt

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points