使用StringBuilder填充字符串中的空格

时间:2015-11-24 19:02:06

标签: java

我尝试编写的这个方法应该接受一个字符串,然后将一个字符串数组插入任何"_"字符的原始字符串中,并在其前面加一个a / a,具体取决于是合适的。如果要插入的字符串是变量,我将使用它,我不知道它应该是a还是a。但它不起作用。例如,如果theString仅为"_"array{pineapple},则会打印a pineapple_。如果theString"I am holding _, which is not a fruit, like _"array{pineapple, apple},则会打印I am holding _, which is not a fruit, like a pinapple_。我看了看,但我找不到问题。我不太确定stringBuilder类如何工作,所以问题可能源于此。谢谢你能给我的任何帮助!

public static void printWithVar(String theString,  String[] array){
    int arrayPosition = 0;
    String insert;
    StringBuilder builder  = new StringBuilder(theString);
    for (int i = 0;i <= theString.length();i++){
        // if a "_" is found
        if (theString.substring(i).equals("_")){
            // if the first letter is a vowel
            if (array[arrayPosition].substring(0).equalsIgnoreCase("a") || array[arrayPosition].substring(0).equalsIgnoreCase("e") || array[arrayPosition].substring(0).equalsIgnoreCase("i") || array[arrayPosition].substring(0).equalsIgnoreCase("o") || array[arrayPosition].substring(0).equalsIgnoreCase("u")){
                builder.deleteCharAt(i);
                insert = "an " + array[arrayPosition];
            }
            // if just an "a"
            else{
                builder.deleteCharAt(i);
                insert = "a " + array[arrayPosition];
            }
            builder = new StringBuilder(theString = theString.substring(0, i) + insert + theString.substring(i));
            arrayPosition++;
            i += insert.length();

            // if there are no more strings to insert
            if (arrayPosition == array.length){
                break;// for loop searching for "_" characters
            }
        }// end if an "_" is found
    }// end loop
    System.out.println(builder.toString());
}// end printWithVar

3 个答案:

答案 0 :(得分:0)

在回复第一条评论时(9000),我将代码更改为此,现在可以正常运行。

public static void printWithVar(String theString,  String[] insertArray){
    String[] splitStrings = theString.split("_");
    String output = "";
    String insert;
    // loop until there are no mmore inserts
    for (int i = 0;i < insertArray.length;i++){
        // if an is appropriate 
        if (insertArray[i].substring(i).equalsIgnoreCase("a") || insertArray[i].substring(i).equalsIgnoreCase("e") || insertArray[i].substring(i).equalsIgnoreCase("i") || insertArray[i].substring(i).equalsIgnoreCase("o") || insertArray[i].substring(i).equalsIgnoreCase("u")){
            insert = "an " + insertArray[i];
        }
        // if a is appropriate
        else{
            insert = "a " + insertArray[i];
        }
        // add everything needed to the output string
        output = output + splitStrings[i] + insert;
    }// end the loop
    // print the resulting string
    System.out.println(output);
}// end printWithVar

答案 1 :(得分:0)

如果您接受替代解决方案,我会使用StringTokenizer

public static void printWithVar(String theString, String[] array) {
    final List<Character> vowels = Arrays.asList(new Character[] { 'a', 'e', 'i', 'o', 'u' });

    StringTokenizer tok = new StringTokenizer(theString, "_", true);
    StringBuilder result = new StringBuilder();
    int i = 0;
    while (tok.hasMoreTokens()) {
        String token = tok.nextToken();
        if (token.equals("_")) {
            if (i >= array.length) {
                continue;
            }
            String replacement = array[i];
            if (vowels.contains(replacement.toLowerCase().charAt(0))) {
                result.append("an " + replacement);
            } else {
                result.append("a " + replacement);
            }
            i++;
        } else {
            result.append(token);
        }
    }
    System.out.println(result.toString());
}

答案 2 :(得分:0)

您可以使用indexOf来返回第一次出现"_"的位置,如果找不到则返回-1。虽然可以找到"_",但可以使用.replace(...)方法将"_"换成字符串数组array[wordArrayPosition]中的单词。

public static void printWithVar(String theString,  String[] array){
    int wordArrayPosition = 0;
    String[] vowels = {"a", "e", "i", "o", "u"};
    StringBuilder builder  = new StringBuilder(theString);

    int indexOfSymbol = builder.indexOf("_");
    while (indexOfSymbol != -1) {

        //Adding a or an to the word
        String word = null;
        if (Arrays.asList(vowels).contains(array[wordArrayPosition].charAt(0))){
            word = "an " +  array[wordArrayPosition];
        } else {
            word = "a " +  array[wordArrayPosition];
        }
        //Checking if the word need to be capitalized
        if ((indexOfSymbol == 0) || (builder.charAt(indexOfSymbol - 1) == '.')) {
            word = "A" + word.substring(1);
        }

        builder.replace(indexOfSymbol, indexOfSymbol + 1, word);
        wordArrayPosition += 1;
        indexOfSymbol = builder.indexOf("_");
    }
    System.out.println(builder.toString());
}