1

私はこの出力を与えるコードを書こうとしています:

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

最初の文字列に 2 番目の文字列が表示される場所を除いて、元の文字が + に置き換えられた文字列を返しますが、コードに問題があります。ここにあります:

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;
}

それに加えて、演算子のStringBuilder代わりにa を使用します。+

于 2013-03-17T14:21:27.830 に答える
1

現在のコードで直面している具体的な問題を教えてください。いずれにせよ、これが私がそれを行う方法です:

  • strのすべてのオカレンスで分割して、wordを形成しString[]ます。
  • この配列をループし、現在の配列の要素の長さに対応する'+'文字数を追加します。newString
  • もちろん、配列の最後の要素にいる場合を除き、同じループ反復で、に追加wordします。newString

これは私が意味するものです:

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();
}

ああ、もう 1 つのヒント: ループ内で文字列に繰り返し追加することは避けてください。必要な場合は、StringBuilder代わりに a を使用してください。


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

私が考えることができる最善かつ最も簡単な方法は、正規表現を使用して replaceAll を実行することです。

一般的な考え方は、2 番目の文字を正規表現で構築し、正規表現と置換文字で 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;
}

メインのテストコード:

/**
 * @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 に答える