saim saim - 7 months ago 17
Android Question

How To replace character in android?

I am try to replace character where come"." it should be replace by "". but its not working for me. please anyone tell me what i am missing in my code?

// that variable is define publically
public class QuizActivity extends Activity {
private static String ans = null;
int j = 0;

//here some functionality

next.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
empty();
checkbox_enable();
Custom_checkbox();

final int arraysize = data.size();

j++;
if(j<arraysize){

String Quiz_no = data.get(j).getQuiz_no();
String Question = data.get(j).getQuestion();
op1 = data.get(j).getAnswer_1().trim().toString();
ans = data.get(j).getAns().trim();
String answer=ans.toString();


for (int k = 0; k < answer.length(); k++) {

if (answer.charAt(k) == '.') {


answer.replace(".", "");



}if (answer.charAt(k) == ' ') {

answer.replace(" ", "");
String temp = answer;
Toast.makeText(QuizActivity.this, "replaced "+temp,

Toast.LENGTH_LONG).show();
}
}


please anyone help me . it is not working for me

Answer

Because replace or replaceAll doesn't overwrite on the string. You should use:

answer = answer.replaceAll(".", "");
answer = answer.replaceAll(" ", "");