写作初衷:由于日常开发经常需要用到很多工具类,经常根据需求自己写也比较麻烦 网上好了一些工具类例如commom.lang3或者hutool或者Jodd这样的开源工具,但是 发现他们之中虽然设计不错,但是如果我想要使用,就必须要引入依赖并且去维护依赖,有些 甚至会有存在版本编译不通过问题,故此想要写作一个每个类都可以作为独立工具类使用 每个使用者只需要复制该类,到任何项目当中都可以使用,所以需要尊从以下两个原则才能 做到.在此诚邀各位大佬参与.可以把各自用过的工具,整合成只依赖JDK,每个类都能够单独 使用的工具.每个人当遇到业务需求需要使用的时候,只需要到这里单独拷贝一个即可使用. 抛弃传统的需要引入依赖的烦恼.让大家一起来解决你所面临的业务问题吧!
遵从两大原则
package *;
/**
* @program: simple_tools
* @description:
* @author: ChenWenLong
* @create: 2019-06-04 14:06
**/
public class WordUtils {
public WordUtils() {
super();
}
/**
* 功能描述:
* 〈将str中的空格替换成分割符',',wrapLength表示从str的最后一个字符往回数的索引位置〉
*
* @params : [str, wrapLength]
* @return : java.lang.String
* @author : cwl
* @date : 2019/6/4 14:10
*/
public static String wrap(String str, int wrapLength) {
return wrap(str, wrapLength, null, false);
}
/**
* 功能描述:
* 〈将newLineStr替换str中的空字符串,warpLength为倒数开始替换的索引位置,wrapLongWords为是否替换长字符串〉
*
* @params : [str, wrapLength, newLineStr, wrapLongWords]
* @return : java.lang.String
* @author : cwl
* @date : 2019/6/4 14:21
*/
public static String wrap(String str, int wrapLength, String newLineStr, boolean wrapLongWords) {
if (str == null) {
return null;
}
if (newLineStr == null) {
newLineStr = ",";
}
if (wrapLength < 1) {
wrapLength = 1;
}
int inputLineLength = str.length();
int offset = 0;
StringBuffer wrappedLine = new StringBuffer(inputLineLength + 32);
while ((inputLineLength - offset) > wrapLength) {
if (str.charAt(offset) == ' ') {
offset++;
continue;
}
int spaceToWrapAt = str.lastIndexOf(' ', wrapLength + offset);
if (spaceToWrapAt >= offset) {
// normal case
wrappedLine.append(str.substring(offset, spaceToWrapAt));
wrappedLine.append(newLineStr);
offset = spaceToWrapAt + 1;
} else {
// really long word or URL
if (wrapLongWords) {
// wrap really long word one line at a time
wrappedLine.append(str.substring(offset, wrapLength + offset));
wrappedLine.append(newLineStr);
offset += wrapLength;
} else {
// do not wrap really long word, just extend beyond limit
spaceToWrapAt = str.indexOf(' ', wrapLength + offset);
if (spaceToWrapAt >= 0) {
wrappedLine.append(str.substring(offset, spaceToWrapAt));
wrappedLine.append(newLineStr);
offset = spaceToWrapAt + 1;
} else {
wrappedLine.append(str.substring(offset));
offset = inputLineLength;
}
}
}
}
// Whatever is left in line is short enough to just pass through
wrappedLine.append(str.substring(offset));
return wrappedLine.toString();
}
/**
* 功能描述:
* 〈首字母大写〉
*
* @params : [str]
* @return : java.lang.String
* @author : cwl
* @date : 2019/6/4 15:07
*/
public static String capitalize(String str) {
return capitalize(str, null);
}
/**
* 功能描述:
* 〈驼峰表示法,delimiters为字母连接符〉
*
* @params : [str, delimiters]
* @return : java.lang.String
* @author : cwl
* @date : 2019/6/4 15:08
*/
public static String capitalize(String str, char[] delimiters) {
int delimLen = (delimiters == null ? -1 : delimiters.length);
if (str == null || str.length() == 0 || delimLen == 0) {
return str;
}
int strLen = str.length();
StringBuffer buffer = new StringBuffer(strLen);
boolean capitalizeNext = true;
for (int i = 0; i < strLen; i++) {
char ch = str.charAt(i);
if (isDelimiter(ch, delimiters)) {
buffer.append(ch);
capitalizeNext = true;
} else if (capitalizeNext) {
buffer.append(Character.toTitleCase(ch));
capitalizeNext = false;
} else {
buffer.append(ch);
}
}
return buffer.toString();
}
/**
* 功能描述:
* 〈首字母大写并且遵从驼峰表示法〉
*
* @params : [str]
* @return : java.lang.String
* @author : cwl
* @date : 2019/6/4 15:11
*/
public static String capitalizeFully(String str) {
return capitalizeFully(str, null);
}
/**
* 功能描述:
* 〈首字母大写并且遵从驼峰表示法,delimiters为空格替换的连接符〉
*
* @params : [str, delimiters]
* @return : java.lang.String
* @author : cwl
* @date : 2019/6/4 15:12
*/
public static String capitalizeFully(String str, char[] delimiters) {
int delimLen = (delimiters == null ? -1 : delimiters.length);
if (str == null || str.length() == 0 || delimLen == 0) {
return str;
}
str = str.toLowerCase();
return capitalize(str, delimiters);
}
/**
* 功能描述:
* 〈首字母小写〉
*
* @params : [str]
* @return : java.lang.String
* @author : cwl
* @date : 2019/6/4 15:13
*/
public static String uncapitalize(String str) {
return uncapitalize(str, null);
}
/**
* 功能描述:
* 〈首字母小写,delimiters替换空格分隔符〉
*
* @params : [str, delimiters]
* @return : java.lang.String
* @author : cwl
* @date : 2019/6/4 15:13
*/
public static String uncapitalize(String str, char[] delimiters) {
int delimLen = (delimiters == null ? -1 : delimiters.length);
if (str == null || str.length() == 0 || delimLen == 0) {
return str;
}
int strLen = str.length();
StringBuffer buffer = new StringBuffer(strLen);
boolean uncapitalizeNext = true;
for (int i = 0; i < strLen; i++) {
char ch = str.charAt(i);
if (isDelimiter(ch, delimiters)) {
buffer.append(ch);
uncapitalizeNext = true;
} else if (uncapitalizeNext) {
buffer.append(Character.toLowerCase(ch));
uncapitalizeNext = false;
} else {
buffer.append(ch);
}
}
return buffer.toString();
}
/**
* 功能描述:
* 〈将str中的大写换成小写,小写换成大写〉
*
* @params : [str]
* @return : java.lang.String
* @author : cwl
* @date : 2019/6/4 15:15
*/
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();
}
/**
* 功能描述:
* 〈简写字符〉
*
* @params : [str]
* @return : java.lang.String
* @author : cwl
* @date : 2019/6/4 15:16
*/
public static String initials(String str) {
return initials(str, null);
}
/**
* 功能描述:
* 〈简写字符,delimiters为分隔符〉
*
* @params : [str, delimiters]
* @return : java.lang.String
* @author : cwl
* @date : 2019/6/4 15:16
*/
public static String initials(String str, char[] delimiters) {
if (str == null || str.length() == 0) {
return str;
}
if (delimiters != null && delimiters.length == 0) {
return "";
}
int strLen = str.length();
char[] buf = new char[strLen / 2 + 1];
int count = 0;
boolean lastWasGap = true;
for (int i = 0; i < strLen; i++) {
char ch = str.charAt(i);
if (isDelimiter(ch, delimiters)) {
lastWasGap = true;
} else if (lastWasGap) {
buf[count++] = ch;
lastWasGap = false;
} else {
// ignore ch
}
}
return new String(buf, 0, count);
}
/**
* 功能描述:
* 〈校验ch是否是分割符〉
*
* @params : [ch, delimiters]
* @return : boolean
* @author : cwl
* @date : 2019/6/4 15:17
*/
private static boolean isDelimiter(char ch, char[] delimiters) {
if (delimiters == null) {
return Character.isWhitespace(ch);
}
for (int i = 0, isize = delimiters.length; i < isize; i++) {
if (ch == delimiters[i]) {
return true;
}
}
return false;
}
}