2013-04-18 34 views
5

如:大寫一個句子的第一個詞串與多個句子

一個String =「這是a.line是.over」

應該站出來爲

「這是一個.line區段is.Over」

我想用串標記者兩次

-first split using"." 

-second split using " " to get the first word 

-then change charAt[0].toUpper 

現在我沒有確定如何使用字符串標記器的輸出作爲另一個輸入?

也我可以使用分割方法來生成陣列東西我試圖

 String a="this is.a good boy"; 
    String [] dot=a.split("\\."); 

     while(i<dot.length) 
    { 
     String [] sp=dot[i].split(" "); 
      sp[0].charAt(0).toUpperCase();// what to do with this part? 
+0

你不需要t他第二次分裂使charAt [0] .toUpper(),一個修剪就足夠了。 – Pino

+0

查看此前[answer](http://stackoverflow.com/questions/1892765/capitalize-first-char-of-each-word-in-a-string-java) – GrahamA

+0

@ Bhaskar-問題有點不同在這裏我有一個字符串與多個句子,所以首先我想拆分句子,然後大寫這個詞..我的困惑是關於如何傳遞到第二個拆分 – kshitij

回答

10

使用StringBuilder,無需分割,創造其他字符串,等等,看到代碼

public static void main(String... args) { 

String text = "this is a.line is. over"; 

int pos = 0; 
boolean capitalize = true; 
StringBuilder sb = new StringBuilder(text); 
while (pos < sb.length()) { 
    if (sb.charAt(pos) == '.') { 
     capitalize = true; 
    } else if (capitalize && !Character.isWhitespace(sb.charAt(pos))) { 
     sb.setCharAt(pos, Character.toUpperCase(sb.charAt(pos))); 
     capitalize = false; 
    } 
    pos++; 
} 
System.out.println(sb.toString()); 
} 
+0

哇!很乾淨!並節省使用不同字符串的麻煩,謝謝! – kshitij

+0

我也需要提取「。」後面的第一個單詞(空格除外)。我可以用什麼來解決這個問題? – kshitij

+0

@kshitij我很高興你喜歡它。 「感謝」在點票和選擇答案方面稍微好一點;) – Vitaly

0

注意,爪哇字符串是不可變(不可修改)。

另請注意,如果在.之後有一個空格(此後第一個字符串將爲空),則sp[0].charAt(0)將導致ArrayIndexOutOfBoundsException

我建議使用char[],所以像:

String a = "this is.a good boy"; 
char arr[] = a.toCharArray(); 
boolean capitalize = true; 
for (int i = 0; i < arr.length; i++) 
    if (arr[i] == '.') 
    capitalize = true; 
    else if (arr[i] != ' ' && capitalize) 
    { 
    arr[i] = Character.toUpperCase(arr[i]); 
    capitalize = false; 
    } 
a = new String(arr); 

Character.isWhitespace(arr[i])可能優選arr[i] != ' '

+0

您需要將第一個字符加上大寫 – smttsp

+0

@smttsp字符串的第一個字符?這是大寫,因爲'capitalize'開始爲'true'。 – Dukeling

+0

哦,你是對的。 – smttsp

0

試試這個大寫句子的第一個字母。我只是對你的代碼做了一些改變。

public static void main(String[] args) { 
    String a = "this is.a good boy"; 
    String[] dot = a.split("\\."); 
    int i = 0; 
    String output = ""; 
    while (i < dot.length) { 
     dot[i] = String.valueOf(dot[i].charAt(0)).toUpperCase() 
       + dot[i].substring(1); 
     output = output + dot[i] + "."; 
     i++; 
    } 
    System.out.println(output); 
} 

輸出:

This is.A good boy. 
+0

謝謝!它工作得很好,但我擔心多次轉換可能會減慢大數據速度。 – kshitij

+0

@kshitij我認爲這是最好的解決方案,如果你使用StringBuilder而不是String來輸出變量。 –

2

無需與分割和拼接的一塌糊塗,你可以就地工作的字符數組:

String s = "this is a.line is .over "; 

char[] cs = s.toCharArray(); 

// make sure to capitalise the first letter in the string 
capitaliseNextLetter(cs, 0); 

for (int i = 0; i < cs.length; i++) { 
    // look for a period 
    if (cs[i] == '.') { 
     // capitalise the first letter after the period 
     i = capitaliseNextLetter(cs, i); 
     // we're assigning to i to skip the characters that 
     // `capitaliseNextLetter()` already looked at. 
    } 
} 

System.out.println(new String(cs)); 

// This will capitalise the first letter in the array `cs` found after 
// the index `i` 
private static int capitaliseNextLetter(char[] cs, int i) { 
    for (; i < cs.length; i++) { 
     // This will skip any non-letter after the space. Adjust the test 
     // as desired 
     if (Character.isAlphabetic(cs[i])) { 
      cs[i] = Character.toUpperCase(cs[i]); 
      return i; 
     } 
    } 
    return cs.length; 
} 
相關問題