我的例外是:
- 句首或标点符号后的大写字母
- 标点符号后加空格
- 缩写后,不要使用大写字母
- 在“-”(带空格)之后,使用大写字母
- 在“-”(无空格)之后,不要大写
我的代码如下,但有例外:
private static char[] PUNCTUATION_MARKS = { '?', '!', ';', '.', '-' };
applyCorrectCase("step 1 - take your car");
applyCorrectCase("reply-to address");
applyCorrectCase("req. start");
applyCorrectCase("alt. del. date [alternate detection date]");
applyCorrectCase("you are.important for us? to continue?here! yes");
String applyCorrectCase(String value) {
String lowerCaseValue = value.toLowerCase();
if (value.contains(". ")) {
lowerCaseValue = value.replace(". ", ".");
}
lowerCaseValue = WordUtils.capitalize(lowerCaseValue, PUNCTUATION_MARKS );
System.out.println(lowerCaseValue.replace(".", ". "));
}
这些是我的结果:
Step 1 - take your car <--- The 't' after the '-' need to be uppercase
Reply-To address <--- The 't' after the '-' need to be lowercase
Req. Start <--- The 's' after the '.' need to be lowercase because it is an abbreviation
Alt. Del. Date [alternate detection date] <--- Both 'd' after the '.' need to be lowercase because it is an abbreviation
You are. Important for us? to continue?Here! yes <--- The 't' after the '?' need to be capital, we need to add an space between '?' and 'H', the 'y' after the '!' need to be uppercase
这些是我的期望:
Step 1 - Take your car
Reply-to address
Req. start
Alt. del. date [alternate detection date]
You are. Important for us? To continue? Here! Yes
有什么想法可以修复我的代码吗?
更新
关于代码if (value.contains(". ")) {
,System.out.println(lowerCaseValue.replace(".", ". "));
我在检查更多标点符号之前就做了这些,现在我有更多它不起作用