Comment detail

LL Golf Hole 2 - 文字列に含まれる単語の最初の文字を大文字にする (Nested Flatten)
普通に正規表現で単語を切り出しています。単語の定義は、「Unicodeの文字カテゴリに属する文字の連続」としています(日本語の正書法の定義とは違います)。大文字は単純に "Upper Case" です("Title Case" ではありません)。

サンプルコードの実行結果は以下です。

LL Day And Night.
日本語abc漢字
Αβγ
A=B+C
Één
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.regex.*;

public class Upper {
    private final static Pattern WORD = Pattern.compile("(\\p{L})(\\p{L}*)");

    public static String toUpper1st(String s) {
        Matcher m = WORD.matcher(s);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String rep = m.group(1).toUpperCase() + m.group(2);
            m.appendReplacement(sb, rep);        
        }
        m.appendTail(sb);
        return sb.toString();
    }
    
    public static void main(String[] args) {
        System.out.println(toUpper1st("LL day and night."));
        System.out.println(toUpper1st("日本語abc漢字"));
        System.out.println(toUpper1st("αβγ"));
        System.out.println(toUpper1st("a=b+c"));
        System.out.println(toUpper1st("één"));
    }
}

Index

Feed

Other

Link

Pathtraq

loading...