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.
i--feeds the unchangedito the method and afterwards decrements the value. It is the same as if you would calldeleteCharAt(i); i--;. The--ioperator does decrement it before feeding the method.--i