大写一个句子的第一个字在多个句子的字符串大写一个句子的第一个字在多个句子的字符串(Capitaliz

2019-05-12 09:42发布

例如:

字符串s =“这是a.line是.over”

应该站出来为

“这是a.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?

Answer 1:

使用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());
}


Answer 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;
}


Answer 3:

试试这个大写句子的第一个字母。 我只是做在你的代码变化不大。

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.


Answer 4:

如果你可以使用Apache的公地lang3 WordUtils ,这样做:

WordUtils.capitalizeFully(text, '.')


Answer 5:

需要注意的是Java中的字符串是不可变的(不可修改)。

还要注意的是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] != ' '



Answer 6:

char[] strArr = str.toCharArray();
        for (int i = 0; i < strArr.length; i++) {
            if (str.charAt(i) == " ".charAt(0) && i + 1 < strArr.length) {
                strArr[i + 1] = String.valueOf(String.valueOf(strArr[i + 1])).toUpperCase().charAt(0);
            }
        }
        System.out.println(String.valueOf(strArr));


文章来源: Capitalize first word of a sentence in a string with multiple sentences