如果字符串包含来自我的String数组中的任何单词,如何从字符串中替换单词

时间:2016-11-03 07:58:21

标签: java android

我想替换与我的String数组中的任何一个匹配的单词,我尝试下面的代码,但它只替换第一个条件,我相信它是因为return语句但我不能找出另一种方法。

public String wordsToCorrect (String word)
        {
            word = word.toLowerCase();
            StringBuilder sb;
            String[] bro =new String[5];
            bro [0] = "bros";
            bro [1] = "bro";
            bro [2] = "bros";
            bro [3] = "broda";
            bro [4] = "brother";
            String[] brother = bro;
            String[] si = new String[6];
            si [0] = "sis";
            si [1] = "sister";
            si [2] = "sista";
            si [3] = "6sta";
            si [4] = "sisy";
            si [5] = "sissy";
            String[] sister = si;
            for ( int i=0;i < brother.length;i ++ )
                {
                    if ( StringUtils.containsIgnoreCase(word, brother [i]) )
                        {
                            StrBuilder replace = new StrBuilder();
                            replace.append(word);
                            replace.append(" ");
                            replace.replaceAll(brother [i], "brother");
                            return replace.toString();
                        }
                }

            for ( int i= 0;i < sister.length;i++   )
                {
                    if ( StringUtils.containsIgnoreCase(word, sister [i]) )
                        {
                            StrBuilder replace = new StrBuilder();
                            replace.append(word);
                            replace.append(" ");
                            replace.replaceAll(sister [i], "sister");
                            return replace.toString();

                        }
                }

3 个答案:

答案 0 :(得分:1)

public String wordsToCorrect (String word)
{
    StringBuilder sb;
    String[] bro =new String[5];
    bro [0] = "bros";
    bro [1] = "bro";
    bro [2] = "bros";
    bro [3] = "broda";
    bro [4] = "brother";
    String[] brother = bro;
    String[] si = new String[6];
    si [0] = "sis";
    si [1] = "sister";
    si [2] = "sista";
    si [3] = "6sta";
    si [4] = "sisy";
    si [5] = "sissy";

    List<String> broList = Arrays.asList(bro);
    List<String> sisList = Arrays.asList(si);
    String[] words = word.split("\\s");
    for(int i=0; i<words.length; i++){
        String singleWord = words[i];
        if(broList.contains(singleWord.toLowerCase())){
            words[i] = "brother";
        }else if(sisList.contains(singleWord.toLowerCase())){
            words[i] = "sister";
        }
    }
    return String.join(" ", words);
}

编辑:补充说明

你的问题不仅仅是循环结束。如果你设法继续你的循环你可以在单词&#34;兄弟&#34;所以你得到&#34; brotherther&#34;。使用我的代码,您首先按空格分割,这样您就可以将每个单词隔离,然后将其与您要查找的单词进行匹配。我使用列表,因为它使用包含更简单。你也不需要ignoreCase,因为你正在做.toLower()(我更喜欢它在contains参数,因为这不会改变原始String上的upperCases)。

此代码适用于使用空格作为分隔符的任何String。如果您还需要使用换行符,则需要修改拆分部分并设法维护分隔符,方法可能与初始类似。

答案 1 :(得分:0)

您正在循环中初始化StrBuilder并每次初始化它,而且返回将从循环/方法退出,并且呼叫已被发送回调用位置。

尝试以下代码

          StrBuilder replace = new StrBuilder();                
          for ( int i=0;i < brother.length;i   )
            {
                if ( StringUtils.containsIgnoreCase(word, brother [i]) )
                    {

                        replace.append(word);
                        replace.append(" ");
                        replace.replaceAll(brother [i], "brother");

                    }
            }
         return replace.toString(); // This return statement will also exit from the method, so seperate the brother and sister replacement in seperate methods/calls

将其置于单独的方法

        StrBuilder replace1 = new StrBuilder();
        for ( int i= 0;i < sister.length;i   )
            {
                if ( StringUtils.containsIgnoreCase(word, sister [i]) )
                    {

                        replace1.append(word);
                        replace1.append(" ");
                        replace1.replaceAll(sister [i], "sister");


                    }
            }
             return replace1.toString();

答案 2 :(得分:0)

我在这段代码中看到了三个问题:1)循环变量没有增加,2)&#34;替换&#34;重新初始化每个循环运行,3)返回结束您的方法。所以试试这个:

            StrBuilder replace = new StrBuilder();                
            for (int i=0;i < brother.length;i++) {
                if (StringUtils.containsIgnoreCase(word, brother [i])) {
                        replace.append(word);
                        replace.append(" ");
                        replace.replaceAll(brother [i], "brother");
                 }
            }
            return replace.toString();

然后对第二个周期进行相同的更改,并使其成为一个单独的方法。