The problem is not the isDigit detection, the problem is builder.insert(i, "\n");!
try this, it works without insert:
@org.junit.Test
public void endodingTest() {
String text = "07:10Ο Σκούμπι Ντου & ο κολλητός....";
StringBuilder builder = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if(Character.isDigit(c)) {
builder.append("\n");
}
builder.append(c);
}
System.out.println(builder.toString());
}
The problem is, that everytime you insert a additional \n in the String builder, every char after that line break becomes moved one char backwards. To correct this you have to count all the linebreakes you already inserted and if you insert a new one you have to insert it at position i + numberOfAlreadyInsertedLineBreaks
(builder.insert(i + numberOfAlreadyInsertedLineBreaks, "\n");)
complete example below)
the second thing of course (but you already know it) is that you have to improve your pattern, so at the end this is the soultion
@org.junit.Test
public void endodingTest() {
String text = "07:10Ο Σκούμπι Ντου & ο κολλητός του07:30Πρωϊνή μελέτη10:15Νηστικοί πράκτορες11:15Σαρίτα, είσαι η ζωή μου12:50Οι ειδήσεις του Star13:45Made in Star15:45Μίλα17:45Ειδήσεις17:50Φώτης - Μαρία live19:45Οι ειδήσεις του Star21:00Ο Χαρί Πότερ και ο ημίαιμος πρίγκιψ00:15Σχολή για απατεώνες01:15Supernatural";
StringBuilder builder = new StringBuilder(text);
int numberOfAlreadyInsertedLineBreaks = 0;
for (int i = 0; i < text.length(); i++) {
if (match(text, i)) {
builder.insert(i + numberOfAlreadyInsertedLineBreaks, '\n');
numberOfAlreadyInsertedLineBreaks++;
}
}
System.out.println(builder.toString());
}
private boolean match(String text, int i) {
return Character.isDigit(text.charAt(i))
&& Character.isDigit(text.charAt(i + 1))
&& text.charAt(i + 2) == ':'
&& Character.isDigit(text.charAt(i + 3))
&& Character.isDigit(text.charAt(i + 4));
}