Skip to content

Commit

Permalink
增加按照单词数分割单词的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenix committed Mar 17, 2016
1 parent cdf69bc commit ae727e4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ buildscript {
jcenter()
}
dependencies {
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2"
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6"
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1"
classpath 'com.netflix.nebula:gradle-extra-configurations-plugin:3.0.3'
}
Expand Down
28 changes: 27 additions & 1 deletion src/main/java/cn/wujc/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static Object stringToObject(String str, Class clz) {
if (Date.class.isAssignableFrom(clz)) {
try {
str = str.trim();
if(str.length() == 10){
if (str.length() == 10) {
return new SimpleDateFormat("yyyy-MM-dd").parse(str);
}
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str);
Expand All @@ -41,4 +41,30 @@ public static Object stringToObject(String str, Class clz) {
public static String defaultIfBlank(final String str, final String defaultStr) {
return StringUtils.isBlank(str) ? defaultStr : str;
}

/**
* 将字符串转换为驼峰形式,例如 is_boy转换为isBoy
*
* @param value 要转换的字符串
* @param startWithLowerCase 转换后的字符串是否以小写开始
* @return
*/
public static String camelize(String value, boolean startWithLowerCase) {
String[] strings = split(value.toLowerCase(), "_");
for (int i = startWithLowerCase ? 1 : 0; i < strings.length; i++) {
strings[i] = capitalize(strings[i]);
}
return StringUtils.join(strings);
}

/**
* 根据单词数分割字符串 abcd,按照2个单词分割为[ab,cd],按照3个单词分割为[abc,d]
*
* @param wordsCount 每几个单词进行分割
* @return
*/
public static String[] split(String str, int wordsCount) {
String regex = "(?<=\\G\\w{" + wordsCount + "})";
return str.split(regex);
}
}

0 comments on commit ae727e4

Please sign in to comment.