簡體   English   中英

Java正則表達式:用一定數量的字符將單詞大寫

[英]java regex: capitalize words with certain number of characters

我正在嘗試將超過5個字符的字符串中的單詞大寫。

我可以使用.length檢索大於5個字符的單詞數,並且可以排除大於5個字符的單詞,但無法將其大寫。

例如 輸入:“我喜歡吃餡餅”

例如 輸出:“我愛吃餡餅”

這是我的代碼:

 public static void main(String[] args) {
    String sentence = "";
    Scanner input = new Scanner(System.in);

    System.out.println("Enter a sentence: ");
    sentence = input.nextLine();
    String[] myString = sentence.split("\\s\\w{6,}".toUpperCase());
     for (String myStrings : myString) {
         System.out.println(sentence);
         System.out.println(myStrings);
         }

如果長度大於5,則以空格分隔輸入句子,並使用intiCap方法:

PS:System.out.print將替換為StringBuilder。

String delim = " ";
String[] myString = sentence.split(delim);

for (int i = 0; i < myString.length; i++) {
    if (i != 0) System.out.print(delim);

    if (myString[i].length() > 5)
        System.out.print(intiCap(myString[i]));
    else
        System.out.print(myString[i]);
}


private static String intiCap(String string) {
    return Character.toUpperCase(string.charAt(0)) + string.substring(1);
}
String sentence = "";
StringBuilder sb = new StringBuilder(sentence.length());
Scanner input = new Scanner(System.in);

System.out.println("Enter a sentence: ");
sentence = input.nextLine();
/*
 * \\s (match whitespace character)
 * (<?g1> (named group with name g1)
 * \\w{6,}) (match word of length 6) (end of g1)
 * | (or)
 * (?<g2> (named group with name g2)
 * \\S+) (match any non-whitespace characters) (end of g2)
 */
Pattern pattern = Pattern.compile("\\s(?<g1>\\w{6,})|(?<g2>\\S+)");
Matcher matcher = pattern.matcher(sentence);

//check if the matcher found a match
while (matcher.find())
{
    //get value from g1 group (null if not found)
    String g1 = matcher.group("g1");
    //get value from g2 group (null if not found)
    String g2 = matcher.group("g2");

    //if g1 is not null and is not an empty string
    if (g1 != null && g1.length() > 0)
    {
        //get the first character of this word and upercase it then append it to the StringBuilder
        sb.append(Character.toUpperCase(g1.charAt(0)));
        //sanity check to stop us from getting IndexOutOfBoundsException
        //check if g1 length is more than 1 and append the rest of the word to the StringBuilder
        if(g1.length() > 1) sb.append(g1.substring(1, g1.length()));
        //append a space
        sb.append(" ");
    }
    //we only need to check if g2 is not null here
    if (g2 != null)
    {
        //g2 is smaller than 5 characters so just append it to the StringBuilder
        sb.append(g2);
        //append a space
        sb.append(" ");
    }
}
System.out.println("Original Sentence: " + sentence);
System.out.println("Modified Sentence: " + sb.toString());

您可以使用以下內容(簡短而甜美的:P):

Pattern p = Pattern.compile("(?=\\b\\w{6,})([a-z])\\w+");
Matcher m = p.matcher(sentence);
StringBuffer s = new StringBuffer();
while (m.find()){
    m.appendReplacement(s, m.group(1).toUpperCase() + m.group(0).substring(1));
}
System.out.println(s.toString());

參見Ideone演示

暫無
暫無

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

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