public static String RemoveChar(String s,char a,char b)
{
StringBuilder sb = new StringBuilder(s);
for(int i=0;i < sb.length();i++)
{
if(sb.charAt(i)==a || sb.charAt(i)==b)
{ System.out.println("Characters removed are : "+a+" "+b);
System.out.println("Removed character at "+i+" : "+s.charAt(i));
sb.deleteCharAt(i);
}
}
return sb.toString();
}
**Input** : RemoveChar("beabeefeab",'a','b');
**Output** : Characters removed are : a b
Removed character at 0 : b
Characters removed are : a b //It checks if the character at the index is a or b
Removed character at 1 : e //after it passes the if condition it removes e.Why does this happen? What alternative can i do for this?
Characters removed are : a b
Removed character 6 : f
ebeefeb
I am new to stringbuilder and java so excuse me if this is a silly question.Suggest an alternative and tell me what went wrong here.It would be really helpful for this beginner :)