2013-04-18 33 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; 
} 
相关问题