1

我正在尝试编写一个将给出此输出的代码:

plusOut("12xy34", "xy") → "++xy++"

它返回一个字符串,其中原始字符已被 + 替换,除了第二个字符串出现在第一个字符串中的位置,但我的代码有问题。这里是:

public String plusOut(String str, String word) {
  String newString = "";
  for (int i=0; i<str.length()-1; i++) {
    if (str.substring(i, word.length()).equals(word)) {
      newString = newString + str.substring(i, word.length());
    }
    else {
      newString = newString + "+";
    }
  }
  return newString;
}
4

6 回答 6

3

您的代码中存在一些错误,请参阅注释。

public String plusOut(String str, String word) {
    String newString = "";
    // iterate up to length() to catch the last char if word.length() is 1
    for (int i = 0; i < str.length(); i++) {
        // use min() to avoid an IndexOutOfRange
        String sub = str.substring(i, Math.min(i+word.length(), str.length()));
        if (sub.equals(word)) {
            newString = newString + sub;
            // skip remaining characters of word
            i += sub.length()-1;
        }
        else {
            newString = newString + "+";
        }
    }
    return newString;
}

除此之外,我会使用 aStringBuilder而不是+运算符。

于 2013-03-17T14:21:27.830 回答
1

您真的应该告诉我们您当前的代码面临哪些具体问题。无论如何,我会这样做:

  • 拆分str所有出现的word以形成String[].
  • 循环遍历该数组并附加一些'+'字符以newString对应于您所在数组的任何元素的长度。
  • 在同一个循环迭代中,追加wordnewString,除非您当然位于数组的最后一个元素上。

这就是我的意思:

public static String plusOut(String str, String word) {
    StringBuilder newString = new StringBuilder(str.length());
    String[] split = str.split(word);

    for (int i = 0; i < split.length; i++) {
        for (int j = 0; j < split[i].length(); j++)
            newString.append('+');

        if (i != split.length - 1)
            newString.append(word);
    }

    return newString.toString();
}

哦,还有一个提示:尽量避免在循环中重复附加到字符串。如果需要,请使用 aStringBuilder代替。


System.out.println(plusOut("12xy34", "xy"));
++xy++
于 2013-03-17T14:22:09.263 回答
0

我能想到的最好和最简单的方法是使用正则表达式并进行replaceAll。

一般的想法是让第二个字符用它构建一个正则表达式,并用正则表达式和替换字符替换所有。

public String plusOut(String str, String word) {

String regEx="[^"+Pattern.quote(word)+"]";

str.replaceAll(regEx,"+");

}

请注意, Pattern.quote() 将确保您的单词不会弄乱正则表达式。

我没有尝试代码,但它应该可以正常工作。

于 2013-03-17T14:21:37.483 回答
0

这将为您做到这一点。

public String plusOut(String str, String word) {

    if(!str.contains(word)){
        System.out.println("Word not found in string!");
        return "Ut-oh!";
    }
    int indexOfStart = str.indexOf(word);

    StringBuilder sb = new StringBuilder();
    for(int i = 0; i<indexOfStart; i++){
        sb.append('+');
    }

    sb.append(word);

    for(int i=indexOfStart+word.length(); i < str.length(); i++){
        sb.append('+');
    }

    return sb.toString();
}
于 2013-03-17T14:21:48.467 回答
0

试试这个 :

public static String plusOut(String word, String find) {
    StringBuilder newStr = new StringBuilder();
    int start = word.indexOf(find);
    if (start > -1) {
        for (int i = 0; i < start; i++) {
            newStr.append("+");
        }
        newStr.append(find);
        for (int i = 0; i < word.length() - (start + find.length()); i++) {
            newStr.append("+");
        }
    }
    return newStr;
}
于 2013-03-17T14:27:25.577 回答
0

这么多答案!好吧,这也是我的:

public static String plusOut(String str, String word) {
    String output = "";
    int index = str.indexOf(word); // if -1 is returned, replace all chars
    for (int i= 0; i < str.length(); i++) {
        if(i == index)
        {
            output += word;
            i += word.length() -1; // because i++ will still occurr
            continue;
        }
        output += "+";
    }
    return output;
}

并在 main 中测试代码:

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    String test = "somethinghello12345.1!#";
    System.out.println(test + " -> " + plusOut(test, "hello"));

    test = "somethinghello12345.1!#";
    System.out.println(test + " -> " + plusOut(test, "not gonna work"));
}

将产生输出:

somethinghello12345.1!# -> +++++++++hello+++++++++
somethinghello12345.1!# -> +++++++++++++++++++++++
于 2013-03-17T14:34:39.227 回答