Simple regular expression:

Pattern p = Pattern.compile("(0)\\s(HEAD)"); Matcher m = p.matcher("0 HEAD"); String d=""; if (m.find()) { d=m.group(2); } if (d=="HEAD") { d="OK"; } 

But, unfortunately, d! = "HEAD". Where is the mistake?

    1 answer 1

    In Java, strings are objects and need to be compared using .equals . In your case, the condition should be as follows:

     if (d.equals("HEAD")) { d = "OK"; } 
    • I also caught these rakes when I started writing in Java. ) - dzhioev
    • and if String a = "a"; String a2 = "a"; a == a2 ?? - Gorets
    • @Gorets, this is likely to be true, since the optimization will put a pointer to an already existing string in a2, but in general it is not guaranteed that two identical strings necessarily have the same pointer. - ReinRaus
    • Thank! Well and after in the lines: how to be with! =,> =, Etc.? - Sergey_New
    • one
      By the way, in C # you can compare strings with == - Gorets