I need to check if the line contains strings that must be
eliminated and indicate which symbols would be eliminated.
A character sequence is replaced by underscores (““), accordingly with the sequence length, if there are three or more contiguous characters with the same symbol. for example, the line “, _, @, @, @, @, $, $, , #, #,!” would be transformed into “, _, _, _, _, _, _, $, $, _, #, #,!” After the process of elimination.
I need to do this only with String or StringBuilder, Regex, ect… (Only Basic coding of Java).
Can’t use arrays also.
Thanks in advance.
This is what i tried:
public static void main(String[] args) {
String linha = "##,$$$$,%%%%,@%@@@,!!!!", validos = "$#%!@";
for (int i = 0; i < validos.length(); i++) {
linha = linha.replaceAll("\\" + validos.charAt(i) + "{3,}", "_");
}
System.out.println (linha);
}
}
The problem here is that replaces a sequence with just one “_”, and i don’t know which chars are replaced.
Surely you can do this in many ways, and probably this is a good exercise to do by yourself. Here you have a basic implementation using just basic loop structures and nothing fancy like StringUtils libraries… Note that your previous loop implementation would have missed several occurrences of the same character repeated in different locations of
linha.