I have the following code:
public static void main(String[] args) {
StringBuilder phoneNumber = new StringBuilder("828-707-5634");
for(int i = 0; i < phoneNumber.length(); i++)
{
if (phoneNumber.charAt(i) == '-')
phoneNumber.deleteCharAt(i--);
}
System.out.print(phoneNumber);
}
Indeed this removes the dash from the phone number, but intuitively I would have expected it to remove the character just before the dash (because of the --). How/why does this work? Additionally, I find that if I use ++ or omit the increment/decrement entirely the dashes are still removed.