簡體   English   中英

我如何將 Java 中單詞的第一個字母大寫?

[英]How would I capitalize the first letter of a word in Java?

我如何將句點 (.) 之后的下一個單詞大寫? 例如。

太陽升起來了。 植物開始生長。

我需要怎么做才能將植物中p大寫?

根據您提出這個問題的方式,我假設您是一般編程的初學者,尤其是 Java 編程的初學者。

所以第一個答案是Java中沒有神奇的解決方案; 即沒有“找到並大寫每個句子的第一個單詞”。 您(或某人)需要通過設計和編寫一些代碼來解決它。

解決問題的方法是將其分解為多個部分。 例如,這是一種可能的故障......

  1. 給定一個輸入字符串,將其拆分為句子字符串。

  2. 給定一個句子字符串,提取第一個單詞。

  3. 給定一個詞,將其大寫。

  4. 用一個新詞替換句子的第一個詞。

  5. 將一個句子序列轉回一個字符串。

然后對上面的代碼進行編碼,並單獨和一起測試它們。 現有的 Java 庫 API 可以為您提供幫助。 例如:

(閱讀每個這些方法的 javadocs ...)

代碼運行后,您可以對其進行改進以使代碼更好(可讀、可維護等)和/或使其運行得更快。

如果您對上述方法有疑問,請隨時提出后續問題。 但是您需要向我們展示您的代碼……或偽代碼。

與其嘗試自己查找單詞和句子的邊界,不如使用BreakIterator類,一個句子實例

BreakIterator 也處理檢測指定語言環境的句子邊界的所有困難,例如數字中的小數點不是句子邊界。

這是一些將轉換句子的示例代碼:

String sentence = "The sun rose.  plants started to grow.";
StringBuilder buf = new StringBuilder(sentence);

BreakIterator bi = BreakIterator.getSentenceInstance(Locale.ENGLISH);
bi.setText(sentence);
int pos;
do
{
    pos = bi.next();
    if (pos != BreakIterator.DONE && pos < buf.length())
        buf.setCharAt(pos, Character.toUpperCase(buf.charAt(pos)));
}
while (pos != BreakIterator.DONE);

String correctedSentence = buf.toString();
System.out.println(correctedSentence);

這是使用正則表達式的解決方案:

public String capitalizeSentences(String s) {
    Pattern p = Pattern.compile("\\.\\s+\\w");
    Matcher m = p.matcher(s);
    StringBuffer buf = new StringBuffer();
    while (m.find()) m.appendReplacement(buf, m.group().toUpperCase());
    m.appendTail(buf);
    return buf.toString();
}

模式/\\.\\s+\\w/匹配一個點、一個或多個空格和一個單詞字符的序列。 正則表達式對於字符串處理非常強大,幾乎所有編程語言都支持。 一次學習他們的語法,您將在所有語言中都更有效率!

編輯:有關使用 Java 的BreakIterator類將文本分成句子的更優雅的解決方案,請參閱@prunge 的解決方案。

嘗試這個。

String phrase = "The sun rose. plants started to grow";

// Split every sentence
String[] sentences = phrase.split(".");

String capitalized = "";

StringBuilder sb = new StringBuilder();

// Process every sentence
for (String str : sentences){
    // Trim the sentence. It might have a space in the beginning
    String result = str.trim();
    // get the first char
    String fc = "" + result.charAt(0);
    // Replace with capitalized one
    result = result.replace(fc, fc.toUpperCase());
    sb.append(result);
    sb.append(". "); // A full-stop and space
}

capitalized = sb.toString();

你能解析整個句子嗎,你的分隔符將是一個句號。 那么從上面的例子來看,你基本上會有 2 個字符串; “太陽升起”和“植物開始生長”,然后您可以獲取每個字符的第二個字符(將句號后的空格作為第一個字符)並將其大寫。

public class mainClass {

public static void main(String[] args) {

    String phrase = " The sun rose. plants started to grow.";
    String delims = "\\."; //full stop as delimiter
    String[] tokens = phrase.split(delims);

    String capitalizedLetter;
    String word;
    String capitalizedWord;

    for(String sentence: tokens)
    {
    capitalizedLetter = sentence.substring(0, 2).toUpperCase();
    word = sentence.substring(2);
    capitalizedWord = capitalizedLetter + word;
    System.out.println(capitalizedWord + "."); //to include the missing full-stop
    } 

}

}

希望這可以幫助! 祝你好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM