mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-07 00:38:46 +00:00
Rewrite capitalize method
This commit is contained in:
@@ -23,27 +23,18 @@ public final class TextUtil {
|
|||||||
* @return The string with correct capitalization.
|
* @return The string with correct capitalization.
|
||||||
*/
|
*/
|
||||||
public static String capitalize(String str) {
|
public static String capitalize(String str) {
|
||||||
char[] chars = str.toCharArray();
|
boolean capitalize = true;
|
||||||
boolean sentenceStart = true;
|
StringBuilder bldr = new StringBuilder(str);
|
||||||
for (int i = 0; i < chars.length; i++) {
|
for (int index = 0, length = str.length(); index < length; index++) {
|
||||||
char c = chars[i];
|
char character = bldr.charAt(index);
|
||||||
if (sentenceStart) {
|
if (character == '.' || character == '!' || character == '?') {
|
||||||
if (c >= 'a' && c <= 'z') {
|
capitalize = true;
|
||||||
chars[i] -= 0x20;
|
} else if (capitalize && !Character.isWhitespace(character)) {
|
||||||
sentenceStart = false;
|
bldr.setCharAt(index, Character.toUpperCase(character));
|
||||||
} else if (c >= 'A' && c <= 'Z') {
|
capitalize = false;
|
||||||
sentenceStart = false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (c >= 'A' && c <= 'Z') {
|
|
||||||
chars[i] += 0x20;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (c == '.' || c == '!' || c == '?') {
|
|
||||||
sentenceStart = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new String(chars, 0, chars.length);
|
return bldr.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user