I want to capitalize some reserved words, but I have problems to do it. These are the strings that I'm using:
id often refers to identity, identification, or an identifier something
the base rate (usd) is a new busda for us
My code below:
String value1 = "id often refers to identity, identification, or an identifier something";
String value2 = "the base rate (usd) is a new busda for us";
applyCorrectCase(value1);
applyCorrectCase(value2);
String applyCorrectCase(String value) {
String[] reservedWords = {"id", "usd"};
String newValue = value;
for (String word : reservedWords) {
if (newValue.contains(word)) {
newValue = newValue.replaceAll(word, word.toUpperCase());
}
}
System.out.println(newValue);
}
This is the result:
ID often refers to IDentity, IDentification, or an IDentifier something
the base rate (USD) is a new bUSDa for us
The problem is that it also capitalizes the letters ID of the word identifier and I do not want that, same for USD and busda.
CodePudding user response:
You can use stream
:
private static List<String> reservedWord = List.of("id", "USD");
public static String applyCorrectCase(String value) {
return Arrays.stream(value.split(" "))
.map(str -> isReserved(str) ? str.toUpperCase() : str)
.collect(Collectors.joining(" "));
}
private static boolean isReserved(String str) {
return Arrays.stream(str.split("[\\(\\)]"))
.anyMatch(reservedWord::contains);
}
Then:
String str1 = "id often refers to identity, identification, or an identifier something";
String str2 = "the base rate (usd) is a new busda for us";
System.out.println(applyCorrectCase(str1));
System.out.println(applyCorrectCase(str2));
Output:
ID often refers to identity, identification, or an identifier something
the base rate (USD) is a new busda for us
CodePudding user response:
The pattern to identify the reserved words should look like this:
"\\b(id|usd)\\b"
, that is, it may be created from the input array of the reserved words where the words are joined inside a group with "|"
.
Next, since Java 9 there is method Matcher::replaceAll
accepting a conversion function and returning a result string. There's no need to explicitly split and rejoin the words in the input string.
So, the words may be capitalized as this:
private static final String[] RESERVED_WORDS = {"id", "usd", "us"};
// build the pattern
private static final Pattern RESERVED = Pattern.compile(
Arrays.stream(RESERVED_WORDS)
.collect(Collectors.joining("|", "\\b(", ")\\b"))
);
static String applyCorrectCase(String value) {
// replace matched group
return RESERVED.matcher(value)
.replaceAll(mr -> mr.group().toUpperCase());
}
Test:
String value1 = "id often refers to identity, identification, or an identifier something";
String value2 = "the base rate (usd) is a new busda for us";
System.out.println(applyCorrectCase(value1));
System.out.println(applyCorrectCase(value2));
Output:
ID often refers to identity, identification, or an identifier something
the base rate (USD) is a new busda for US
CodePudding user response:
String applyCorrectCase(String value) {
Set<String> reservedWords = Set.of("id", "(usd)", "usd");
StringJoiner sb = new StringJoiner(" ");
Arrays.stream(value.split(" ")).forEach(word -> {
if (reservedWords.contains(word)) {
sb.add(word.toUpperCase());
} else sb.add(word);
});
return sb.toString();
}