-1

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

sajid Hussain
  • 385
  • 1
  • 6
  • 15

4 Answers4

1

simply use replaceAll on String you want to replace.

For Example

String test = "hello.say.something";
test.replaceAll(".","");
Toast.makeText(getApplicationContext(),test,Toast.LENGTH_SHORT).show();

Result : hellosaysomething

Happy coding..

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
1

A possible duplicate of How to Replace dot (.) in a string in Java.

So, instead of looping through your answer-string and checking each character, you can simply use .replaceAll. So change your code into something like this:

...
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();
  //no need to loop through answer-string
  answer = answer.replaceAll(".","");
}

Hope this helps.

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
0

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

answer = answer.replaceAll(".", "");
answer = answer.replaceAll(" ", "");
Burak Day
  • 907
  • 14
  • 28
0

You should know that Strings are immutable, so you need to store the output of answer.replace(".", ""); in some new variable or to the same variable, So do like this

 String replacedString = null;

 for (int k = 0; k < answer.length(); k++) {
    if (answer.charAt(k) == '.') {
        replacedString = answer.replace(".", "");

    }if (answer.charAt(k) == ' ') {
        replacedString  = answer.replace(" ", "");
    }
}

OR

answer = answer.replace(".", "");
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68