简体   繁体   English

Android equalsIgnoreCase无法正常工作

[英]Android equalsIgnoreCase not working correctly

temp1 is "accountant"editText is "accountant" temp1是“会计” editText是“会计”

but i checked with String.length(); 但是我检查了String.length();

  • temp1 is a string has 11 characters editText has 10 characters temp1是具有11个字符的字符串editText具有10个字符

I tried trim() method, i got the same result again. 我尝试过trim()方法,再次得到了相同的结果。

Code sample: 代码示例:

    if(temp1.equalsIgnoreCase(editText1.getText().toString().trim()))
      {
        //do someting
      }

I solved. 我解决了

  • I coverted source file to UTF-8 with "Notepad++" and get words from new source file. 我用“ Notepad ++”将源文件覆盖到UTF-8,并从新源文件中获取单词。 EqualsIgnoreCase works now. EqualsIgnoreCase现在可以使用。

Since temp1 is a string and has 11 characters, you must have placed a whitespace after the word accountant. 由于temp1是一个字符串,具有11个字符,因此您必须在会计一词后放置一个空格。 (Either that or you flat out misspelled the word.) Accountant is only 10 characters long, so there must be whitespace. (或者您拼写错误的单词。)会计只有10个字符长,因此必须有空格。 (Well, it's pretty guaranteed since you, yourself, created it.) (很好,因为您自己创建了它就可以保证。)

Either remove the whitespace from temp1 , or trim temp1 before you compare. 在比较之前,请从temp1删除空白,或修剪temp1 Also, double-check temp1 to actually make sure you spelled the word correctly. 另外,请仔细检查temp1以确保您正确拼写了该单词。

You may try this code : 您可以尝试以下代码:

    //put this below public class activity

    int index=0; 
    String [] answer ={"Accountant"};

and put this button code below setContentView 并将此按钮代码放在setContentView下面

buttonanswer = (Button)findViewById(R.id.yourbutton);
buttonanswer.setOnClickListener(this);

and then put this code 然后把这段代码

    @Override
    public void onClick(View v) {               

    edittextanswer=(EditText)findViewById(R.id.youreditText); 
    if(v==buttonanswer) {

        String myanswer=edittextanswer.getText().toString();              
        if(myanswer.equalsIgnoreCase(answer[index]))
        {
            //do someting
        }
        else
        {    
           //do someting
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM