如何在第一次出现时从字符串中删除特定单词

时间:2014-10-01 05:13:46

标签: java string

我的代码出现问题。 它适用于大多数情况下,例如拍摄' cool'出于以下情况:Nagash很酷很酷,变成Nagash真的很酷。 但是像是在采取什么样的行为呢?出于以下情况:Nagash很酷,变成Nagash很酷。

我是java的新手,我的代码必须用循环编写,没有快捷方法。如果您的回复可以请你回答如何用java编写的代码。

String deleteFromString(String word, String remove) {       
    String updated = "";    

    int count = 0;
    int start = 0;
    int end = 0;

    if (stringEquals(remove, "")) {
        updated = word;
        return updated;
    }

    while (count < length(word)) {
        if (getChar(word, count) == getChar(remove, start)) {
            if (getChar(word, count + length(remove) - 1) == getChar(remove, length(remove) - 1)) {
                start = count;
                end = count + length(remove) - 1; 


                println(count);
                println(count + length(remove) - 1);
                count = length(word);
            }
        }
        count++;
    }

    count = 0;
    while (count < start - 1) {
        updated += getChar(word, count);
        count++;

    }

    while (end < length(word)) {
        updated += getChar(word, end);
        end++;

    }
    println(updated);   


    return updated;        
}

3 个答案:

答案 0 :(得分:0)

您可以参考StringUtils替换方法的源代码。


public static String replace(final String text, final String searchString, final String replacement, int max) {
    if (isEmpty(text) || isEmpty(searchString) || replacement == null || max == 0) {
        return text;
    }
    int start = 0;
    int end = text.indexOf(searchString, start);
    if (end == INDEX_NOT_FOUND) {
        return text;
    }
    final int replLength = searchString.length();
    int increase = replacement.length() - replLength;
    increase = increase < 0 ? 0 : increase;
    increase *= max < 0 ? 16 : max > 64 ? 64 : max;
    final StringBuilder buf = new StringBuilder(text.length() + increase);
    while (end != INDEX_NOT_FOUND) {
        buf.append(text.substring(start, end)).append(replacement);
        start = end + replLength;
       if (--max == 0) {
            break;
        }
        end = text.indexOf(searchString, start);
   }
    buf.append(text.substring(start));
    return buf.toString();
}


public static boolean isEmpty(CharSequence cs) {
    return cs == null || cs.length() == 0;
}

答案 1 :(得分:0)

你的问题在于最后两个循环。在这两个中的第一个中,循环将在您删除&#39;之前终止两个字符。 substring而不是之前的字符。在第二个循环中,循环开始于&#39; end&#39;角色,因此包括它。在您的示例中,包含最后一个字符,因为最终字符的索引总是比字符串的长度小一个。这样可以将删除的子字符串中的一个字符移动到父字符串中的左侧。请尝试改为:

while (count < start) {
    updated += getChar(word, count);
    count++;

}

while (end + 1 < length(word)) {
    updated += getChar(word, end + 1);
    end++;

}

答案 2 :(得分:-1)

为了简单起见,请这样做:

 String test="Nagash is coolish";
 test=test.replaceFirst("ish", "");
 System.out.println(test);

输出

Nagash is cool

编辑: 以下是处理strings替换数量的完整代码。

public class Test {
  public static void main(String args[]) throws Exception {
      Test test=new Test();
      String result=test.computeRemove("this is demo and demo", "demo", "one");
      // you can pass different parameters for desired result.
      System.out.println(result);
  }
  public String computeRemove(String main,String remove, String replace) {
      main=main.replaceFirst(remove,replace);
      return main;
  }
}

有关此类String操作的详情,请访问this链接。

相关问题