Codingbat plusOut - StringBuffer麻烦

时间:2015-02-25 15:28:43

标签: java stringbuffer

给定一个字符串和一个非空字符串,返回原始字符串的一个版本,其中所有字符都被字符串(“+”)替换,但字符串的外观保持不变。

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

我在尝试为此问题编写StringBuffer解决方案时遇到问题。 这是原始代码:

public static String plusOut(String str, String word) {
    int i = 0; 
    String str2 = "";
    while (i < str.length() - word.length()+1) 
        if (!str.substring(i,i+word.length()).equals(word)) {
            str2 += "+";
            i++;
        }
        else {
            str2 += word;
            i += word.length(); //found pattern - skip
        }
    //if any remaining chars at end (guaranteed not to be pattern) replace    
    //with +s
    if (i < str.length() && !str.substring(i).equals(word.substring(1))) {
        for (int j = 0; j < word.length()-1; j++) str2 += "+";
    }
    return str2;
}

6 个答案:

答案 0 :(得分:0)

public String plusOut(String str, String word) {
      String result = "";
      int i = 0 ;

      while(i < str.length() ) {
         if (str.substring(i).startsWith(word)) {
            result = result + word;
            i = i + word.length();
         } else {
            result = result + "+" ;
            i++;
         }
      }

      return result ;
}

// StringBuilder的

public String plusOut(String str, String word) {
    StringBuilder strBuilder = new StringBuilder();
    int i = 0 ;

      while(i < str.length() ) {
         if (str.substring(i).startsWith(word)) {
            strBuilder.append(word);
            i = i + word.length();
         } else {
            strBuilder.append("+");
            i++;
         }
      }

      return strBuilder.toString();

}

答案 1 :(得分:0)

以下是我提出的建议:

  public String plusOut(String str, String word) 
  {
    //Create a blank string.
    String out = "";
    //Create a counter to get the end amount.
    int endAmt = 0;
    //We can only manipulate from 0 to str.length() - word.length() in this for loop, because
    //when searching for word in str, we can't go beyond the bounds.
    for (int i = 0; i < str.length() - word.length(); i++)
    {
      //If we find word in str,
        if (str.substring(i, i + word.length()).equals(word))
        {
          //We append word and increment 2 times further.
          out = out + str.substring(i, i + word.length());
          //increment i by 2 (one here, and one at the  end)
          i += word.length() - 1;
          //We set the endAmt equal to the next iteration for future use when getting the rest
          //of the amount later.
          endAmt = i + 1;
        }
        //if it doesn't match, we change the character to a '+'
        else
        {
          out = out.substring(0,i) + '+';
        }
    }
    //Now, we finish the string after the searching for word in str.
    //If the last substring is word, we add word to the mix.
    if (str.substring(str.length() - word.length()).equals(word))
    {
      out = out + word;
    }
    //If it's not, then we refactor the string to after then last instance of word
    else
    {
      out = out.substring(0, endAmt);
      //and then we add the remaining amount of +'s
      for (int i = 0; i < str.length() - endAmt; i++)
      {
        out = out + "+";
      }
    }
    //finally, we return the string.
    return out;
  }

答案 2 :(得分:0)

这是一个非常简化且易于理解的代码

public static String plusOut(String str,String word){
    StringBuffer b = new StringBuffer();
    int indexOfWord = str.indexOf(word, 0);
    for (int i = 0; i < str.length(); i++) {
        if(i==indexOfWord){
            b.append(word);
            i=i+word.length()-1;//move index by word length 
            //get next index for the word
            indexOfWord = str.indexOf(word,indexOfWord+word.length());
        }else{
            b.append("+");
        }
    }
    return b.toString();
}

答案 3 :(得分:0)

public String plusOut(String str, String word) {
  String temp = str.replace(word, "+");
  String newStr = "";

  for (int i=0; i<temp.length(); i++)
  {
    if (temp.charAt(i) == '+')
    {
      newStr += word;
    }
    else
    {
      newStr += "+";
    }
  }

  return newStr;
}

答案 4 :(得分:0)

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

答案 5 :(得分:0)

public String plusOut(String str, String word) {
  String newStr = "";
  int sLength = str.length();
  int wLength = word.length();
  
  for (int i = 0; i < sLength; i++){
    if (i <= (sLength-wLength) && str.substring(i, i+wLength).equals(word)){
      newStr += word;
      i += (wLength-1);
      continue;
    }
    newStr += "+";
  }
  return newStr;
}