Here is the code that’s causing the confusion:
public class MyClass{
public static void main(String args[]){
String s="one\ntwo\nthree\n";
s=s.replaceAll("^","START");
s=s.replaceAll("$","END");
System.out.print(s);}}
Here is the output:
STARTone
two
threeEND
END
I know that the dollar sign matches the end of the line in Java, but I thought it would act the same even when the string ends with \n. Why was “END” printed twice?