Viennarz Valdevieso Curtiz Viennarz Valdevieso Curtiz - 2 months ago 13
Android Question

Cannot Resolve Method 'deleteCharAt()'

What is the alternative for 'deleteCharAt()'? Is there a library?

I wanted to delete a specific character on a String.

Here is my current code:

public class MainActivity extends Activity {

@Override


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

EditText name1;
EditText name2;
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v){


EditText name1;
EditText name2;
TextView textout2;
TextView textout;
int sum;

name1 = (EditText)findViewById(R.id.editText);
name2 = (EditText)findViewById(R.id.editText2);
int totalLength = 0;
int total;
String inputText1 = name1.getText().toString();
String inputText2 = name2.getText().toString();
totalLength = name1.length() + name2.length();

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.TOP | Gravity.LEFT,0, 0);

for (int i = 0; i < name1.length(); i++)
{

char ch = inputText1.charAt(i);
for (int j = 0; j < name2.length(); j++)
{
if (ch == inputText2.charAt(j))
{
inputText1.deleteCharAt();
inputText2.deleteCharAt();

}

}

}


});



}

Answer

Just change these lines:

StringBuilder inputText1 = new StringBuilder(name1.getText().toString());
StringBuilder inputText2 = new StringBuilder(name2.getText().toString());

StringBuilder class has a deleteCharAt() method(exactly what you are looking for).

To get a String object from StringBuilder, simply use:

String str = inputText1.toString();