【问题标题】:StringBuilder insert methodStringBuilder 插入方法
【发布时间】:2016-10-24 23:27:32
【问题描述】:

我试图在每个子字符串的开头插入一个字符,而不是在子字符串的位置 0 处插入,该方法是将字符插入到整个字符串的开头。例如:

Input: esttar apple%hc orange%hc annanabar eachpar
Expected output: test apple orange banana peach
Actual output: pbtest apple orange anana each

除了 StringBuilder 插入方法之外,一切都按预期工作。我的代码如下。非常感谢。

    private String decryptText(String encrypted, String cipher){
    StringBuilder stringBuilder = new StringBuilder(); //for manipulating the substrings
    StringBuilder builder2 = new StringBuilder(); //StringBuilder object for returning and accumulating instances of decrypted
    String decryptedSS = "";

    cipher = cipher.replaceAll("-", ""); //replaces all the hyphens
    char[] cipherKey = cipher.toCharArray(); //converts the cipherKey to a character array
    char y1 = cipherKey[2];


    String[] strArr = encrypted.split(" "); //splits the string into an array

    for(String subStr : strArr){ //for each substring in the string array

        stringBuilder.append(subStr); //copies the substring into a String Builder object

        stringBuilder = stringBuilder.deleteCharAt(stringBuilder.length()-1); //deletes the last character
        stringBuilder = stringBuilder.deleteCharAt(stringBuilder.length()-1); //deletes the last character

        char first = stringBuilder.charAt(stringBuilder.length()-1); //copies the last character for prepending to the word if the word started with a consonant


        if(stringBuilder.charAt(stringBuilder.length()-1) == y1){ //if the last character is equal to y1
            stringBuilder.deleteCharAt(stringBuilder.length()-1); //delete it

        }
        else{ //******The problem resides in this else statement
            stringBuilder.deleteCharAt(stringBuilder.length()-1); //delete the last character
            stringBuilder.insert(0, first); //insert the copied character at the beginning of the substring
        }

        stringBuilder.append(" "); //appends a space to each word
        decryptedSS = stringBuilder.toString(); //converts the StringBuilder object to a string
    }

    builder2.append(decryptedSS); //appends the decrypted substring to the StringBuilder object to concatenate the string

    String decrypted = builder2.toString(); //converts the StringBuilder object to a string

    return decrypted; //returns the decrypted string
}

【问题讨论】:

  • 我想补充一点,如果您还没有,请尝试通过调试器运行代码。 Eclipse 有一个不错的开箱即用。这将帮助您在错误发生时发现错误。

标签: java string substring stringbuilder arrayofstring


【解决方案1】:

这行是问题所在:

stringBuilder.insert(0, first); //insert the copied character at the beginning of the substring

这里的stringBuilder 不代表当前子字符串,而是您尝试构建的最终解密字符串。这就是为什么该值被添加到整个字符串的开头。

您有多种选择:

  1. 您可以将循环末尾的stringBuilder 的结果字符串添加到builder2,然后清除stringBuilder。从您的代码编写方式来看,我想这就是您所期望的:

    builder2.append(decryptedSS); 
    stringBuilder.setLength(0); 
    

    请注意,您还将删除循环外的builder2.append(decryptedSS);

  2. 您可以在循环开始时保存 stringBuilder 的长度,然后插入该值而不是 0。

  3. 您可以在循环中完全修改subStr,并将完成的版本附加到循环末尾的stringBuilder

【讨论】:

  • 非常感谢!这是非常有见地的。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多