I have some code removing the BOM header of the first line of a file like this :
public static String removeBOMIndicator(String line) {
if (line.length() > 1) {
byte[] bytes = line.getBytes();
if (bytes.length >= 3 && bytes[0] == (byte) 0xEF && bytes[1] == (byte) 0xBB && bytes[2] == (byte) 0xBF) {
line = line.substring(1);
}
}
return line;
}
This function works well and I created a test case to be sure it stays that way. The test passes without trouble when I launch it with IntelliJ or when our SonarQube instance runs it.
However, when I launch the test using Git Bash (mvn surefire:test -Dtest=RemoveBomHeadertTest
), the output of my function contains two characters ╗┐
at the start.
If I change my code to remove the 3 first characters instead of only the first one, then it works well in Git Bash, but in IntelliJ, I'm missing the first two characters of my String.
Any idea why the behaviour of substring
might be different in these two cases ?