如果匹配则替换文本中的单词

时间:2022-09-13 09:49:30

I want to take what ever word entered and replace it with the word "hi" I am only trying to make it replace on word put. When I run it, it crashed.

我想把任何单词输入并用“hi”这个单词替换它我只是试图将它替换为单词put。当我运行它时,它崩溃了。

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

    final EditText r=(EditText)findViewById(R.id.editText1);
    String t=r.getText().toString();
    String[] t1= t.split(" ");


    for(int i=0;i<=t1.length;i++)
    {
        if(t[i].equals("hi") )
        {
            TextView uu=(TextView)findViewById(R.id.textView2);
            uu.setText(t[i]);
        }
    }
}

7 个解决方案

#1


1  

Just change t[i] to t1[i]

只需将t [i]更改为t1 [i]

try this

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

    final EditText r=(EditText)findViewById(R.id.editText1);
    String t=r.getText().toString();
    String[] t1= t.split(" ");


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

       if(t1[i].equals("hi") )
      {
         TextView uu=(TextView)findViewById(R.id.textView2);
         uu.setText(t1[i]);
      }


    }

 }

#2


1  

replace t1[] instead of t[] inside if block.

如果阻塞,则替换t1 []而不是t []。

try this:

if(t1[i].equals("hi") ) instead of if(t[i].equals("hi") )

if(t1 [i] .equals(“hi”))而不是if(t [i] .equals(“hi”))

#3


0  

Without the actual error we cannot say much, that being said, this segment:

没有实际的错误,我们不能说太多,这就是说,这段:

for(int i=0;i<=t1.length;i++)
{    
    if(t[i].equals("hi") )
    {
        TextView uu=(TextView)findViewById(R.id.textView2);
        uu.setText(t[i]);
    }
}

Will yield an ArrayOutOfBoundsException since in Java, arrays are 0 based and the last accessible array location is [arraySize] - 1.

将产生一个ArrayOutOfBoundsException,因为在Java中,数组是基于0的,最后一个可访问的数组位置是[arraySize] - 1。

Changing it to this:

将其更改为:

for(int i=0;i<t1.length;i++)
{    
    if(t[i].equals("hi") )
    {
        TextView uu=(TextView)findViewById(R.id.textView2);
        uu.setText(t[i]);
    }
}

Should resolve the issue (at least for this particular exception).

应该解决问题(至少对于这个特殊的例外)。

That however, should iterate only over the characters making up the phrase, so it should never match hi. Changing t[i] to t1[i] in here: if(t1[i].equals("hi") ) and possibly here: uu.setText(t1[i]); should yield a better solution.

但是,应该只对构成短语的字符进行迭代,因此它永远不应该匹配hi。在这里改变t [i]到t1 [i]:if(t1 [i] .equals(“hi”))并且可能在这里:uu.setText(t1 [i]);应该产生更好的解决方案。

Just as an FYI, using meaningful names never hurts.

就像一个FYI一样,使用有意义的名字永远不会伤害。

#4


0  

why are you initialising everytime

你为什么每次都在初始化

TextView uu=(TextView)findViewById(R.id.textView2);

keep this in oncreate() once

将它保存在oncreate()中一次

#5


0  

I found some mistakes in your code. Don't need split. I think your code should be as

我在你的代码中发现了一些错误。不需要拆分。我认为你的代码应该是

final EditText r=(EditText)findViewById(R.id.editText1);
TextView uu=(TextView)findViewById(R.id.textView2);
String t=r.getText().toString();
  if(t.contains("hi"))
    {
        // do your stuff
    }
  else{
   //false
  }

#6


0  

You said that you want to replace whatever word entered with the word "hi". But you are checking if the word (I presume that instead of t[i] you wanted to say t1[i]) is equal to "hi" and then you want to put it in the 2nd textView? You are just going to get the number of "hi"s in your original input. If you want to change ANY word to "hi", just remove the if test and you'll get as many "hi"s as word you have entered.

你说你要用“hi”这个词替换输入的任何单词。但是你正在检查这个词(我认为不是t [i]你想说t1 [i])等于“hi”然后你想把它放在第二个textView中吗?您只需要在原始输入中获​​得“hi”的数量。如果您想将任何单词更改为“hi”,只需删除if测试,您将获得与您输入的单词一样多的“hi”。

#7


0  

t[i].equals("hi")

Compares objects. replace it with t1[i].tostring()=="Hi"

比较对象。用t1 [i]替换它.tostring()==“你好”

#1


1  

Just change t[i] to t1[i]

只需将t [i]更改为t1 [i]

try this

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

    final EditText r=(EditText)findViewById(R.id.editText1);
    String t=r.getText().toString();
    String[] t1= t.split(" ");


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

       if(t1[i].equals("hi") )
      {
         TextView uu=(TextView)findViewById(R.id.textView2);
         uu.setText(t1[i]);
      }


    }

 }

#2


1  

replace t1[] instead of t[] inside if block.

如果阻塞,则替换t1 []而不是t []。

try this:

if(t1[i].equals("hi") ) instead of if(t[i].equals("hi") )

if(t1 [i] .equals(“hi”))而不是if(t [i] .equals(“hi”))

#3


0  

Without the actual error we cannot say much, that being said, this segment:

没有实际的错误,我们不能说太多,这就是说,这段:

for(int i=0;i<=t1.length;i++)
{    
    if(t[i].equals("hi") )
    {
        TextView uu=(TextView)findViewById(R.id.textView2);
        uu.setText(t[i]);
    }
}

Will yield an ArrayOutOfBoundsException since in Java, arrays are 0 based and the last accessible array location is [arraySize] - 1.

将产生一个ArrayOutOfBoundsException,因为在Java中,数组是基于0的,最后一个可访问的数组位置是[arraySize] - 1。

Changing it to this:

将其更改为:

for(int i=0;i<t1.length;i++)
{    
    if(t[i].equals("hi") )
    {
        TextView uu=(TextView)findViewById(R.id.textView2);
        uu.setText(t[i]);
    }
}

Should resolve the issue (at least for this particular exception).

应该解决问题(至少对于这个特殊的例外)。

That however, should iterate only over the characters making up the phrase, so it should never match hi. Changing t[i] to t1[i] in here: if(t1[i].equals("hi") ) and possibly here: uu.setText(t1[i]); should yield a better solution.

但是,应该只对构成短语的字符进行迭代,因此它永远不应该匹配hi。在这里改变t [i]到t1 [i]:if(t1 [i] .equals(“hi”))并且可能在这里:uu.setText(t1 [i]);应该产生更好的解决方案。

Just as an FYI, using meaningful names never hurts.

就像一个FYI一样,使用有意义的名字永远不会伤害。

#4


0  

why are you initialising everytime

你为什么每次都在初始化

TextView uu=(TextView)findViewById(R.id.textView2);

keep this in oncreate() once

将它保存在oncreate()中一次

#5


0  

I found some mistakes in your code. Don't need split. I think your code should be as

我在你的代码中发现了一些错误。不需要拆分。我认为你的代码应该是

final EditText r=(EditText)findViewById(R.id.editText1);
TextView uu=(TextView)findViewById(R.id.textView2);
String t=r.getText().toString();
  if(t.contains("hi"))
    {
        // do your stuff
    }
  else{
   //false
  }

#6


0  

You said that you want to replace whatever word entered with the word "hi". But you are checking if the word (I presume that instead of t[i] you wanted to say t1[i]) is equal to "hi" and then you want to put it in the 2nd textView? You are just going to get the number of "hi"s in your original input. If you want to change ANY word to "hi", just remove the if test and you'll get as many "hi"s as word you have entered.

你说你要用“hi”这个词替换输入的任何单词。但是你正在检查这个词(我认为不是t [i]你想说t1 [i])等于“hi”然后你想把它放在第二个textView中吗?您只需要在原始输入中获​​得“hi”的数量。如果您想将任何单词更改为“hi”,只需删除if测试,您将获得与您输入的单词一样多的“hi”。

#7


0  

t[i].equals("hi")

Compares objects. replace it with t1[i].tostring()=="Hi"

比较对象。用t1 [i]替换它.tostring()==“你好”