I am learning Java, and in a task I have to replace some but not all occurrences of a '-' to a '(' and same for '*' a ')', only if the inside of the - is an id as as follows:
String input = "-id1234* -id1235* -id1236* Do not replace these -signs*";
String output = "(id1234) (id1235) (id1236) Do not replace these -signs*";
I have tried iterating through a loop, and replacing the first occurrence once the for loop goes to a position with i, but that doesn't seem to work:
static String clean(String input) {
String tmp = input;
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == 'i') {
tmp = input.replaceFirst("-", "(");
} else if(Character.isDigit(i)){
tmp = input.replaceFirst("*", ")");
}
}
return tmp;
}