尝试使用indexOf位置来大写字符串

时间:2017-03-13 02:59:55

标签: java

编辑:

我试图将所有代码编译成一个程序。现在我需要的是弄清楚如何从方法返回修改后的字符串。为了达到这个目的,我需要做些什么?

这是新代码:

        public class String_Fixer {

            public static void main(String[] args) {

                String userInput, newString;
                Scanner keyboard = new Scanner(System.in);
                System.out.println("This program takes sentences and capitalizes them for you.");
                System.out.println("Please write a few sentences.");
                System.out.println("");
                userInput = keyboard.nextLine();
                System.out.println("");


                System.out.println("Here is the original sentence:");
                System.out.println(userInput);
                System.out.println("");
                System.out.println("And here is the modified sentence:");
                System.out.println();  
            }

            public static List<Integer> stringFixer(String userInput) {
                List<Integer> indexes = new ArrayList<>();
                int first = 0, offset = 2, index, offset_index;
                while ((index = userInput.indexOf('.', first)) != -1) {
                    offset_index = index + offset;
                    if (offset_index < userInput.length()) {
                        indexes.add(offset_index);
                    }
                    first = index + 1;
                }
                while ((index = userInput.indexOf('?', first)) != -1) {
                    offset_index = index + offset;
                    if (offset_index < userInput.length()) {
                        indexes.add(offset_index);
                    }
                    first = index + 1;
                }
                return indexes;
            }

            public static String CapatilizeChars(List<Integer> indexes, String string) {
                StringBuilder newString = new StringBuilder(string);
                for (int i : indexes) {
                    char capChar = Character.toUpperCase(newString.charAt(i));
                    newString.setCharAt(i, capChar);
                }
                return newString.toString();
            }

        }

1 个答案:

答案 0 :(得分:0)

使用Character.toUpperCase(CHARACTER);方法获取字符的大写变体。

有关查找字符串中字符的所有索引的信息,请参阅此帖子:Indexes of all occurrences of character in a string

编辑:

以下是我将如何解决这个问题。首先使用此方法获取需要更改的索引:

public static List<Integer> getIndexesOf(String regex, String stringToCheck, int offset) {

    List<Integer> indexes = new ArrayList<>();

    // Where to begin each indexof operation
    int first = 0;

    // The direct indexof result
    int index;
    // The indexof result offset by the specified amount
    int offset_index;

    // While we are still getting results from index of, save result under index and loop
    while ((index = stringToCheck.indexOf(regex, first)) != -1) {

        offset_index = index + offset;

        // If the offset index doesn't excede the strings length add it to list
        if (offset_index < stringToCheck.length()) {

            indexes.add(offset_index);

        }

        first = index + 1;

    }

    return indexes;

}

然后使用此方法大写每个索引:

public static String CapatilizeChars(List<Integer> indexes, String string) {

    StringBuilder newString = new StringBuilder(string);

    // For each index capatilize the specified char
    for (int i : indexes) {

        char capChar = Character.toUpperCase(newString.charAt(i));

        newString.setCharAt(i, capChar);

    }

    return newString.toString();

}

这不是最快的方法,但它的优点是该方法比特定的capitalize_char_after_regex方法更有用。您可以通过在第二个方法的for循环中获取代码并将其放入第一个方法中,轻松地将其组合到一个方法中。

编辑2:

修复您的更新代码:

替换:StringFixer modifiedString = new StringFixer();

使用:String modifiedString = StringFixer.CapatilizeChars(StringFixer.getIndexesOf(userInput), userInput);

并从regex方法中删除getIndexesOf参数,因为您未在应用程序中使用它们。

我给你的方法是静态的,这意味着它们不与对象的实例相关联。换句话说,您不需要构造StringFixer类来使用它们。