6

例如:

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

应该出来

“这是一个.Line is.Over”

我想过两次使用字符串标记器

-first split using"."

 -second split using " " to get the first word

 -then change charAt[0].toUpper

现在我不确定如何使用字符串标记器的输出作为另一个输入?

我也可以使用 split 方法生成我尝试过的数组

     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?
4

6 回答 6

13

使用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());
}
于 2013-04-18T09:06:38.250 回答
3

无需搞乱拆分和拼接,您可以在字符数组上就地工作:

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;
}
于 2013-04-18T09:38:12.550 回答
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.
于 2013-04-18T09:08:27.197 回答
1

如果您可以使用Apache commons-lang3中的 WordUtils ,请执行以下操作:

WordUtils.capitalizeFully(text, '.')
于 2015-06-08T19:42:55.990 回答
0

请注意,Java 字符串是不可变的(不可修改)。

另请注意,如果在 a 之后直接有一个空格(从那时起第一个字符串将为空) ,sp[0].charAt(0)则会导致a。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] != ' '

于 2013-04-18T08:59:34.490 回答
0
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));
于 2018-12-05T10:54:47.133 回答