【发布时间】:2021-11-18 10:31:44
【问题描述】:
我的例外是:
- 句首或标点符号后的大写字母
- 在标点符号后添加空格
- 缩写后不要使用大写字母
- 在“-”(带空格)之后,使用大写字母
- 在“-”(无空格)之后,不要大写
我的代码如下:
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(".", ". ")); 我在检查更多标点符号之前就做了这些,现在我有更多它不起作用
【问题讨论】:
-
可能是因为标点符号后面有空格(例如空格是大写的)?
-
什么意思?对于这个例子:“reply-to address” 我需要显示这样的文本:“Reply-to address”,只有“R”大写字母因为是句子的开头,但“to”需要保持小写,因为它是一个复合词。例如“step 1 - take your car”,“S”必须是大写字母,因为它是句子的开头,“T”必须是大写字母,因为它位于标点符号和空格之后
标签: java string java-8 apache-commons-text