如何大写字符串中每个单词的第一个和最后一个字母
我这样做了 -
String cap = "";
for (int i = 0; i < sent.length() - 1; i++)
{
if (sent.charAt(i + 1) == ' ')
{
cap += Character.toUpperCase(sent.charAt(i)) + " " + Character.toUpperCase(sent.charAt(i + 2));
i += 2;
}
else
cap += sent.charAt(i);
}
cap += Character.toUpperCase(sent.charAt(sent.length() - 1));
System.out.print (cap);
当第一个单词超过单个字符
时,它不起作用请使用简单的功能,因为我是初学者
答案 0 :(得分:4)
使用apache commons lang
library变得非常容易:
String testString = "this string is needed to be 1st and 2nd letter-uppercased for each word";
testString = WordUtils.capitalize(testString);
testString = StringUtils.reverse(testString);
testString = WordUtils.capitalize(testString);
testString = StringUtils.reverse(testString);
System.out.println(testString);
ThiS StrinG需要1sT和2nD字母大写字母EacH 字
答案 1 :(得分:2)
您应该使用空格将String拆分为字符分隔符,然后为每个标记在第一个和最后一个字符上应用toUpperCase()并创建一个新的String作为结果。
非常简单的样本:
String cap = "";
String sent = "hello world. again.";
String[] token = sent.split("\\s+|\\.$");
for (String currentToken : token){
String firstChar = String.valueOf(Character.toUpperCase(currentToken.charAt(0)));
String between = currentToken.substring(1, currentToken.length()-1);
String LastChar = String.valueOf(Character.toUpperCase(currentToken.charAt(currentToken.length()-1)));
if (!cap.equals("")){
cap += " ";
}
cap += firstChar+between+LastChar;
}
当你执行许多连接时,你应该支持在String上使用StringBuilder。
输出结果:HellO World. AgaiN
答案 2 :(得分:0)
您的代码缺少第一个单词的第一个字母。我会把这视为一种特殊情况,即
class Fixture {
def fixtureAA() { "code aa" }
def fixtureBB() { "code bb" }
def fixtureCC() { "code cc" }
}
class SampleFixture {}
SampleFixture.metaClass.static.invokeMethod = { String method, args ->
def code = method - "aFixtureWithCode"
new Fixture()."fixture${code}"()
}
assert SampleFixture.aFixtureWithCodeAA() == "code aa"
assert SampleFixture.aFixtureWithCodeBB() == "code bb"
assert SampleFixture.aFixtureWithCodeCC() == "code cc"
当然,有更简单的方法可以做你正在做的事情。
答案 3 :(得分:0)
基本上,您只需迭代所有字符并在满足以下条件之一时替换它们:
如果出于性能和内存原因使用StringBuilder
(不要在String
将要执行的每次迭代中创建+=
),它可能如下所示:
StringBuilder sb = new StringBuilder( "some words in a list even with longer whitespace in between" );
for( int i = 0; i < sb.length(); i++ ) {
if( i == 0 || //rule 1
i == (sb.length() - 1 ) || //rule 2
Character.isWhitespace( sb.charAt( i - 1 ) ) || //rule 3
Character.isWhitespace( sb.charAt( i + 1 ) ) ) { //rule 4
sb.setCharAt( i, Character.toUpperCase( sb.charAt( i ) ) );
}
}
结果:SomE WordS IN A LisT EveN WitH LongeR WhitespacE IN BetweeN
如果你想检查其他规则(例如标点符号等),你可以创建一个方法来调用前一个和下一个字符,并检查所需的属性。
答案 4 :(得分:0)
String stringToSearch = "this string is needed to be first and last letter uppercased for each word";
// First letter upper case using regex
Pattern firstLetterPtn = Pattern.compile("(\\b[a-z]{1})+");
Matcher m = firstLetterPtn.matcher(stringToSearch);
StringBuffer sb = new StringBuffer();
while(m.find()){
m.appendReplacement(sb,m.group().toUpperCase());
}
m.appendTail(sb);
stringToSearch = sb.toString();
sb.setLength(0);
// Last letter upper case using regex
Pattern LastLetterPtn = Pattern.compile("([a-z]{1}\\b)+");
m = LastLetterPtn.matcher(stringToSearch);
while(m.find()){
m.appendReplacement(sb,m.group().toUpperCase());
}
m.appendTail(sb);
System.out.println(sb.toString());
output:
ThiS StrinG IS NeedeD TO BE FirsT AnD LasT LetteR UppercaseD FoR EacH WorD