deleteCharAt() not deleting character in stringbuilder all of sudden in loop -
i need delete chars stringbuilder couple not deleted reason. have far: have method string , arraylist paramaters. string i'm passing following: "beabeefeab" , arraylist contains following 6 character combinations of length 2: ab, ae, af, be, bf, ef. name of arraylist combos arraylistcombos , walk through arraylist in outside loop , individual chars of string combo can compare individual chars string char in stringbuilder. inside loop walk through stringbuilder , compare modstring.charat(x) firstchar taken combo. @ first combo ab example, firstchar=a , secondchar=b. able delete first e string "beabeefeab"but 2 consecutive e's don't deleted , following string: "babeeab" , should "babab" a's , b's deleted. can - appreciate.
for(int w=0;w<arraylistcombos.size();w++){ string tempcombotokeep = arraylistcombos.get(w); //split char in combo , store independantly in order compare char firstchar = tempcombotokeep.charat(0); system.out.println("first char: "+firstchar); char secondchar = tempcombotokeep.charat(1); system.out.println("second char: "+secondchar); stringbuilder modstring = new stringbuilder(s); //system.out.println("here stringbuilder before modify: "+modstring.tostring()); //walk through stringbuilder find individual chars , remove rest for(int x=0;x<modstring.length();x++){ //if first char not equal 1 of combos, delete if (modstring.charat(x) != firstchar){ system.out.println("char not equal firstchar: "+modstring.charat(x)+" "+firstchar); //the char inside stringbuilder not equal either of combo chars need removed if (modstring.charat(x) != secondchar){ system.out.println("char not equal secondchar either!!! " + "delete char string builder: "+modstring.charat(x)); modstring.deletecharat(x); } } }
i figured out problem. looping through stringbuilder, deleting characters stringbuilder , trying use modified stringbuilder in loop go through characters. fix created new stringbuilder , used append method , appended each time needed keep character , allowed me continue looping original stringbuilder don't miss characters.
Comments
Post a Comment