如何替换android中的角色?

时间:2016-05-30 08:56:17

标签: java android str-replace

我试着替换来自哪里的角色"。"它应该被""取代。但它不适合我。请有人告诉我我的代码中缺少什么?

       // 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();
                                        }
                                    }
请任何人帮助我。它不适合我

4 个答案:

答案 0 :(得分:1)

只需在要替换的String上使用replaceAll

例如

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

结果:hellosaysomething

快乐编码..

答案 1 :(得分:1)

How to Replace dot (.) in a string in Java的可能副本。

因此,您可以简单地使用.replaceAll,而不是遍历您的答案字符串并检查每个字符。因此,将代码更改为以下内容:

...
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(".","");
}

希望这有帮助。

答案 2 :(得分:0)

因为replacereplaceAll没有覆盖字符串。 你应该使用:

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

答案 3 :(得分:0)

您应该知道Strings是不可变的,因此您需要将answer.replace(".", "");的输出存储在某个新变量或同一个变量中,所以这样做

 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(" ", "");
    }
}

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