将每个单词的每个首字母大写在一个字符串中,该字符串包含在markdown中的星号中。允许在星号中使用多个单词

时间:2021-03-22 21:45:50

I have a Spring blog. In the Post model, I wrote a method that capitalizes each first letter of each word in a Post's title. This works fine. However, the input field when creating the title allows for bold and italic options via a markdown editor, which then wraps words in asterisks. This is where the issues arise.

我有一个Spring博客。在Post模型中,我写了一个方法,将Post的标题中每个单词的每个首字母大写。这很好用。但是,创建标题时的输入字段允许通过降价编辑器进行粗体和斜体选项,然后将标记文字包含在星号中。这就是问题出现的地方。

A bold or italicized word gets capitalized as long as it's the only word wrapped in asterisks. But, if two or more words are conveniently wrapped all together, like a book or movie title that has spaces in between words, it breaks and says "java.lang.StringIndexOutOfBoundsException: String index out of range:"

只要它是包含在星号中的唯一单词,大胆或斜体的单词就会被大写。但是,如果两个或多个单词被方便地包装在一起,就像在单词之间有空格的书或电影标题,它会断开并说“java.lang.StringIndexOutOfBoundsException:String index out of range:”

In the if statements, I've tried using word.charAt(i) == ' ' to check if there's an empty space but I can't seem to figure it out as sometimes it will capitalize the second word after the space, like "word Word" but then the first word gets neglected.

在if语句中,我尝试使用word.charAt(i)==''来检查是否有空格,但我似乎无法弄明白,因为有时它会占用空格后的第二个单词,就像“单词Word”然后第一个单词被忽略了。

I simply want to capitalize every word so that

我只是想把每一个字都弄清楚

italics: *word word*
bold: **word word**
both: ***word word***

returns Word Word , Word Word, or both respectively.

分别返回Word Word,Word Word或两者。

Is this even a good approach? Any help is greatly appreciated! Thank you in advance.

这甚至是一个好方法吗?任何帮助是极大的赞赏!先感谢您。

 public String makeTitleUppercase(String title) {

    StringBuffer sb = new StringBuffer();

    String[] sentence = title.split(" ");

    for (String word : sentence) {
        char[] letters = word.trim().toCharArray();

        //Capitalize each first letter of each word (works):
        letters[0] = Character.toUpperCase(letters[0]);

        //Capitalizing bold and italicized markdown (issues):
        for (int i = 0; i < letters.length; i++) {

            //word.charAt(i) == ' ' where???

            // *italics*:
            if (word.charAt(i) == '*') {
                letters[1] = Character.toUpperCase(letters[1]);

                //**bold**:
                if (word.charAt(i + 1) == '*') {
                    letters[2] = Character.toUpperCase(letters[2]);
                }

                //***both***  
                if (word.charAt(i + 2) == '*') {
                    letters[3] = Character.toUpperCase(letters[3]);
                }
                break;
            }
        }

        word = new String(letters);
        sb.append(word).append(" ");
        System.out.println("get here");
    }
    return sb.toString().trim();
}

1 个解决方案

#1


1  

You've gotten yourself a bit into trouble by focusing on the details of the markdown formatting. Note in particular that you scan each word for asterisks regardless of whether you've already successfully capitalized the first letter; this results in your exception when between one and three asterisks appear at the end of the word.

通过关注降价格式的细节,你已经陷入了麻烦。请特别注意,无论您是否已成功将第一个字母大写,您都会扫描每个单词中的星号;如果在单词的末尾出现一到三个星号,则会导致异常。

You should generalize your characterization a bit, so that the formatting is not handled as a special case. For instance, the rule you want might be "in each whitespace-delimited word, skip any asterisks and capitalize the next character, if any". After the tokenization, that might look like this:

您应该稍微概括一下您的特性,以便格式化不作为特殊情况处理。例如,您想要的规则可能是“在每个以空格分隔的单词中,跳过任何星号并将下一个字符大写,如果有的话”。标记化后,可能如下所示:

        char[] letters = word.toCharArray(); // no need to trim()

        for (int i = 0; i < letters.length; i++) {
            if (letters[i] != '*') {
                // Capitalize the first non-asterisk (even if that doesn't change it)
                letters[i] = Character.toUpperCase(letters[i]);
                // No need to look any further
                break;
            }
        }
        // That's it for capitalizing!

Special cases complicate your reasoning. Sometimes they cannot be avoided, but when you have the choice to just be more general, it's usually a win.

特殊情况使您的推理变得复杂。有时它们是无法避免的,但是当你可以选择更加通用时,它通常是一种胜利。

#1


1  

You've gotten yourself a bit into trouble by focusing on the details of the markdown formatting. Note in particular that you scan each word for asterisks regardless of whether you've already successfully capitalized the first letter; this results in your exception when between one and three asterisks appear at the end of the word.

通过关注降价格式的细节,你已经陷入了麻烦。请特别注意,无论您是否已成功将第一个字母大写,您都会扫描每个单词中的星号;如果在单词的末尾出现一到三个星号,则会导致异常。

You should generalize your characterization a bit, so that the formatting is not handled as a special case. For instance, the rule you want might be "in each whitespace-delimited word, skip any asterisks and capitalize the next character, if any". After the tokenization, that might look like this:

您应该稍微概括一下您的特性,以便格式化不作为特殊情况处理。例如,您想要的规则可能是“在每个以空格分隔的单词中,跳过任何星号并将下一个字符大写,如果有的话”。标记化后,可能如下所示:

        char[] letters = word.toCharArray(); // no need to trim()

        for (int i = 0; i < letters.length; i++) {
            if (letters[i] != '*') {
                // Capitalize the first non-asterisk (even if that doesn't change it)
                letters[i] = Character.toUpperCase(letters[i]);
                // No need to look any further
                break;
            }
        }
        // That's it for capitalizing!

Special cases complicate your reasoning. Sometimes they cannot be avoided, but when you have the choice to just be more general, it's usually a win.

特殊情况使您的推理变得复杂。有时它们是无法避免的,但是当你可以选择更加通用时,它通常是一种胜利。