Return phone with every dash removed.
removeDashes("555-867-5309") returns "5558675309".
10 strips: H1 to H10
int dash = copy.indexOf("-"); H1copy = copy.substring(0, dash) + copy.substring(dash); H2} H3dash = copy.indexOf("-"); H4} H5public static String removeDashes(String phone) { H6return copy; H7copy = copy.substring(0, dash) + copy.substring(dash + 1); H8String copy = phone; H9while (dash != -1) { H10Order: H6, H9, H1, H10, H8, H4, H3, H7, H5. Extra: H2
public static String removeDashes(String phone) {
String copy = phone;
int dash = copy.indexOf("-");
while (dash != -1) {
copy = copy.substring(0, dash) + copy.substring(dash + 1);
dash = copy.indexOf("-");
}
return copy;
} Extra strip: copy = copy.substring(0, dash) + copy.substring(dash);. copy.substring(dash) still starts with the dash, so the dash is never removed and the loop never ends.