📄 wordutils.java
字号:
* </pre>
*
* @param str the String to capitalize, may be null
* @param delimiters set of characters to determine capitalization
* @return capitalized String, <code>null</code> if null String input
* @see #uncapitalize(String)
* @see #capitalizeFully(String)
*/
public static String capitalize(String str, char[] delimiters) {
if (str == null || str.length() == 0) {
return str;
}
int strLen = str.length();
StringBuffer buffer = new StringBuffer(strLen);
int delimitersLen = 0;
if(delimiters != null) {
delimitersLen = delimiters.length;
}
boolean capitalizeNext = true;
for (int i = 0; i < strLen; i++) {
char ch = str.charAt(i);
boolean isDelimiter = false;
if(delimiters == null) {
isDelimiter = Character.isWhitespace(ch);
} else {
for(int j=0; j < delimitersLen; j++) {
if(ch == delimiters[j]) {
isDelimiter = true;
break;
}
}
}
if (isDelimiter) {
buffer.append(ch);
capitalizeNext = true;
} else if (capitalizeNext) {
buffer.append(Character.toTitleCase(ch));
capitalizeNext = false;
} else {
buffer.append(ch);
}
}
return buffer.toString();
}
/**
* <p>Capitalizes all the whitespace separated words in a String.
* All letters are changed, so the resulting string will be fully changed.</p>
*
* <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
* A <code>null</code> input String returns <code>null</code>.
* Capitalization uses the unicode title case, normally equivalent to
* upper case.</p>
*
* <pre>
* WordUtils.capitalize(null) = null
* WordUtils.capitalize("") = ""
* WordUtils.capitalize("i am FINE") = "I Am Fine"
* </pre>
*
* @param str the String to capitalize, may be null
* @return capitalized String, <code>null</code> if null String input
*/
public static String capitalizeFully(String str) {
return capitalizeFully(str, null);
}
/**
* <p>Capitalizes all the delimiter separated words in a String.
* All letters are changed, so the resulting string will be fully changed.</p>
*
* <p>The delimiters represent a set of characters understood to separate words.
* The first string character and the first non-delimiter character after a
* delimiter will be capitalized. </p>
*
* <p>A <code>null</code> input String returns <code>null</code>.
* Capitalization uses the unicode title case, normally equivalent to
* upper case.</p>
*
* <pre>
* WordUtils.capitalize(null) = null
* WordUtils.capitalize("") = ""
* WordUtils.capitalize("i am FINE") = "I Am Fine"
* </pre>
*
* @param str the String to capitalize, may be null
* @param delimiters set of characters to determine capitalization
* @return capitalized String, <code>null</code> if null String input
*/
public static String capitalizeFully(String str, char[] delimiters) {
if (str == null || str.length() == 0) {
return str;
}
str = str.toLowerCase();
return capitalize(str, delimiters);
}
/**
* <p>Uncapitalizes all the whitespace separated words in a String.
* Only the first letter of each word is changed.</p>
*
* <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
* A <code>null</code> input String returns <code>null</code>.</p>
*
* <pre>
* WordUtils.uncapitalize(null) = null
* WordUtils.uncapitalize("") = ""
* WordUtils.uncapitalize("I Am FINE") = "i am fINE"
* </pre>
*
* @param str the String to uncapitalize, may be null
* @return uncapitalized String, <code>null</code> if null String input
* @see #capitalize(String)
*/
public static String uncapitalize(String str) {
return uncapitalize(str, null);
}
/**
* <p>Uncapitalizes all the whitespace separated words in a String.
* Only the first letter of each word is changed.</p>
*
* <p>The delimiters represent a set of characters understood to separate words.
* The first string character and the first non-delimiter character after a
* delimiter will be uncapitalized. </p>
*
* <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
* A <code>null</code> input String returns <code>null</code>.</p>
*
* <pre>
* WordUtils.uncapitalize(null) = null
* WordUtils.uncapitalize("") = ""
* WordUtils.uncapitalize("I Am FINE") = "i am fINE"
* </pre>
*
* @param str the String to uncapitalize, may be null
* @param delimiters set of characters to determine uncapitalization
* @return uncapitalized String, <code>null</code> if null String input
* @see #capitalize(String)
*/
public static String uncapitalize(String str, char[] delimiters) {
if (str == null || str.length() == 0) {
return str;
}
int strLen = str.length();
int delimitersLen = 0;
if(delimiters != null) {
delimitersLen = delimiters.length;
}
StringBuffer buffer = new StringBuffer(strLen);
boolean uncapitalizeNext = true;
for (int i = 0; i < strLen; i++) {
char ch = str.charAt(i);
boolean isDelimiter = false;
if(delimiters == null) {
isDelimiter = Character.isWhitespace(ch);
} else {
for(int j=0; j < delimitersLen; j++) {
if(ch == delimiters[j]) {
isDelimiter = true;
break;
}
}
}
if (isDelimiter) {
buffer.append(ch);
uncapitalizeNext = true;
} else if (uncapitalizeNext) {
buffer.append(Character.toLowerCase(ch));
uncapitalizeNext = false;
} else {
buffer.append(ch);
}
}
return buffer.toString();
}
/**
* <p>Swaps the case of a String using a word based algorithm.</p>
*
* <ul>
* <li>Upper case character converts to Lower case</li>
* <li>Title case character converts to Lower case</li>
* <li>Lower case character after Whitespace or at start converts to Title case</li>
* <li>Other Lower case character converts to Upper case</li>
* </ul>
*
* <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
* A <code>null</code> input String returns <code>null</code>.</p>
*
* <pre>
* StringUtils.swapCase(null) = null
* StringUtils.swapCase("") = ""
* StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
* </pre>
*
* @param str the String to swap case, may be null
* @return the changed String, <code>null</code> if null String input
*/
public static String swapCase(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return str;
}
StringBuffer buffer = new StringBuffer(strLen);
boolean whitespace = true;
char ch = 0;
char tmp = 0;
for (int i = 0; i < strLen; i++) {
ch = str.charAt(i);
if (Character.isUpperCase(ch)) {
tmp = Character.toLowerCase(ch);
} else if (Character.isTitleCase(ch)) {
tmp = Character.toLowerCase(ch);
} else if (Character.isLowerCase(ch)) {
if (whitespace) {
tmp = Character.toTitleCase(ch);
} else {
tmp = Character.toUpperCase(ch);
}
} else {
tmp = ch;
}
buffer.append(tmp);
whitespace = Character.isWhitespace(ch);
}
return buffer.toString();
}
public static boolean endsWith(String str, char c) {
if(str.length() == 0){
return false;
}
if(str.charAt(str.length()-1) == c){
return true;
}
return false;
}
public static boolean endsWith(StringBuffer str, char c) {
if(str.length() == 0){
return false;
}
if(str.charAt(str.length()-1) == c){
return true;
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -