将句子中的第一个单词用多个句子大写 [英] Capitalize first word of a sentence in a string with multiple sentences

查看:87
本文介绍了将句子中的第一个单词用多个句子大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

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

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

应该来out as as

should come out as

这是a.Line is.Over

"This is a.Line is.Over"

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

I thought of using string tokenizer twice

-first split using"."

 -second split using " " to get the first word

 -then change charAt[0].toUpper

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

now i'm not sure how to use the output of string tokenizer as input for another?

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

also i can using the split method to generate array something i tried

     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?


推荐答案

使用StringBuilder,无需拆分并创建其他字符串,依此类推,请参阅代码

Use StringBuilder, no need to split and create other strings, and so on, see the code

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

这篇关于将句子中的第一个单词用多个句子大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆