add method

This commit is contained in:
Looly 2020-03-29 10:07:59 +08:00
parent 9ea0e983bd
commit bcd773b85a
3 changed files with 514 additions and 524 deletions

View File

@ -1,13 +1,12 @@
package cn.hutool.core.lang;
import static java.lang.System.out;
import java.util.Scanner;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.StrUtil;
import java.util.Scanner;
import static java.lang.System.err;
import static java.lang.System.out;
/**
* 命令行控制台工具方法类<br>
@ -189,7 +188,7 @@ public class Console {
* @since 5.2.5
*/
public static String where() {
StackTraceElement stackTraceElement = new Throwable().getStackTrace()[1];
final StackTraceElement stackTraceElement = new Throwable().getStackTrace()[1];
final String className = stackTraceElement.getClassName();
final String methodName = stackTraceElement.getMethodName();
final String fileName = stackTraceElement.getFileName();

View File

@ -1,14 +1,5 @@
package cn.hutool.core.util;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.text.MessageFormat;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import cn.hutool.core.comparator.VersionComparator;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
@ -19,11 +10,24 @@ import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.text.StrSpliter;
import cn.hutool.core.text.TextSimilarity;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Pattern;
/**
* 字符串工具类
*
* @author xiaoleilu
*
*/
public class StrUtil {
@ -74,6 +78,7 @@ public class StrUtil {
public static final String EMPTY_JSON = "{}";
// ------------------------------------------------------------------------ Blank
/**
* 字符串是否为空白 空白的定义如下 <br>
* 1为null <br>
@ -171,6 +176,7 @@ public class StrUtil {
}
// ------------------------------------------------------------------------ Empty
/**
* 字符串是否为空空的定义如下:<br>
* 1为null <br>
@ -218,8 +224,8 @@ public class StrUtil {
*
* @param str 被检查的字符串
* @return 原字符串或者空串
* @since 4.6.3
* @see #nullToEmpty(CharSequence)
* @since 4.6.3
*/
public static String emptyIfNull(CharSequence str) {
return nullToEmpty(str);
@ -247,7 +253,6 @@ public class StrUtil {
*
* @param str 要转换的字符串
* @param defaultStr 默认字符串
*
* @return 字符串本身或指定的默认字符串
*/
public static String nullToDefault(CharSequence str, String defaultStr) {
@ -266,7 +271,6 @@ public class StrUtil {
*
* @param str 要转换的字符串
* @param defaultStr 默认字符串
*
* @return 字符串本身或指定的默认字符串
* @since 4.1.0
*/
@ -286,7 +290,6 @@ public class StrUtil {
*
* @param str 要转换的字符串
* @param defaultStr 默认字符串
*
* @return 字符串本身或指定的默认字符串
* @since 4.1.0
*/
@ -396,6 +399,7 @@ public class StrUtil {
}
// ------------------------------------------------------------------------ Trim
/**
* 除去字符串头尾部的空白如果字符串是<code>null</code>依然返回<code>null</code>
*
@ -411,7 +415,6 @@ public class StrUtil {
* </pre>
*
* @param str 要处理的字符串
*
* @return 除去头尾空白的字符串如果原字串为<code>null</code>则返回<code>null</code>
*/
public static String trim(CharSequence str) {
@ -491,7 +494,6 @@ public class StrUtil {
* </pre>
*
* @param str 要处理的字符串
*
* @return 除去空白的字符串如果原字串为<code>null</code>或结果字符串为<code>""</code>则返回 <code>null</code>
*/
public static String trimStart(CharSequence str) {
@ -514,7 +516,6 @@ public class StrUtil {
* </pre>
*
* @param str 要处理的字符串
*
* @return 除去空白的字符串如果原字串为<code>null</code>或结果字符串为<code>""</code>则返回 <code>null</code>
*/
public static String trimEnd(CharSequence str) {
@ -526,7 +527,6 @@ public class StrUtil {
*
* @param str 要处理的字符串
* @param mode <code>-1</code>表示trimStart<code>0</code>表示trim全部 <code>1</code>表示trimEnd
*
* @return 除去指定字符后的的字符串如果原字串为<code>null</code>则返回<code>null</code>
*/
public static String trim(CharSequence str, int mode) {
@ -732,7 +732,7 @@ public class StrUtil {
* @since 5.1.1
*/
public static boolean contains(CharSequence str, CharSequence searchStr) {
if(null == str || null == searchStr){
if (null == str || null == searchStr) {
return false;
}
return str.toString().contains(searchStr);
@ -901,7 +901,7 @@ public class StrUtil {
final String getOrSetMethodNameStr = getOrSetMethodName.toString();
if (getOrSetMethodNameStr.startsWith("get") || getOrSetMethodNameStr.startsWith("set")) {
return removePreAndLowerFirst(getOrSetMethodName, 3);
} else if(getOrSetMethodNameStr.startsWith("is")) {
} else if (getOrSetMethodNameStr.startsWith("is")) {
return removePreAndLowerFirst(getOrSetMethodName, 2);
}
return null;
@ -1308,6 +1308,7 @@ public class StrUtil {
}
// ------------------------------------------------------------------------------ Split
/**
* 切分字符串
*
@ -1390,7 +1391,7 @@ public class StrUtil {
*/
public static String[] splitToArray(CharSequence str, char separator, int limit) {
if (null == str) {
return new String[] {};
return new String[]{};
}
return StrSpliter.splitToArray(str.toString(), separator, limit, false, false);
}
@ -1517,7 +1518,7 @@ public class StrUtil {
*/
public static String[] split(CharSequence str, CharSequence separator) {
if (str == null) {
return new String[] {};
return new String[]{};
}
final String separatorStr = (null == separator) ? null : separator.toString();
@ -1534,7 +1535,7 @@ public class StrUtil {
*/
public static String[] split(CharSequence str, int len) {
if (null == str) {
return new String[] {};
return new String[]{};
}
return StrSpliter.splitByLength(str.toString(), len);
}
@ -1884,7 +1885,7 @@ public class StrUtil {
/**
* 截取指定字符串中间部分不包括标识字符串<br>
*
* <p>
* 栗子
*
* <pre>
@ -1927,7 +1928,7 @@ public class StrUtil {
/**
* 截取指定字符串中间部分不包括标识字符串<br>
*
* <p>
* 栗子
*
* <pre>
@ -1950,7 +1951,7 @@ public class StrUtil {
/**
* 截取指定字符串多段中间部分不包括标识字符串<br>
*
* <p>
* 栗子
*
* <pre>
@ -1968,45 +1969,43 @@ public class StrUtil {
* </pre>
*
* @param str 被切割的字符串
* @param regexBefore 截取开始的字符串标识
* @param regexAfter 截取到的字符串标识
* @param prefix 截取开始的字符串标识
* @param suffix 截取到的字符串标识
* @return 截取后的字符串
* @author dahuoyzs
* @since 5.2.5
*/
public static String[] subBetweenAll(CharSequence str, CharSequence regexBefore, CharSequence regexAfter) {
if (str == null || regexBefore == null || regexAfter == null || str.length() < 1 || regexBefore.length() < 1 || regexAfter.length() < 1) {
public static String[] subBetweenAll(CharSequence str, CharSequence prefix, CharSequence suffix) {
if(hasEmpty(str, prefix, suffix)) {
return new String[0];
}
final String before = regexBefore.toString().replace("\\", "");
final String after = regexAfter.toString().replace("\\", "");
final Integer beforeNumber = StrUtil.count(str, before);
final Integer afterNumber = StrUtil.count(str, after);
if (beforeNumber<1||afterNumber<1){
final int prefixCount = count(str, prefix);
final int suffixCount = count(str, suffix);
if (prefixCount < 1 || suffixCount < 1) {
return new String[0];
}
LinkedList<String> betweenList = new LinkedList<>();
if (beforeNumber.compareTo(afterNumber) > 0) {
String[] fragments = str.toString().split(regexAfter.toString());
if (prefixCount > suffixCount) {
String[] fragments = split(str, suffix);
for (int i = 0; i < fragments.length - 1; i++) {
String fragment = fragments[i];
if (fragment.contains(before)) {
int beforeIndex = StrUtil.lastIndexOf(fragment, before, 0, false);
if (fragment.contains(prefix)) {
int beforeIndex = StrUtil.lastIndexOf(fragment, prefix, 0, false);
String between = fragment.substring(beforeIndex);
if (between.length()>0)
if (between.length() > 0)
betweenList.add(between);
}
}
} else {
String[] fragments = str.toString().split(regexBefore.toString());
String[] fragments = split(str, prefix);
for (int i = 1; i < fragments.length; i++) {
String fragment = fragments[i];
if (fragment.contains(after)) {
int afterIndex = StrUtil.indexOf(fragment, after, 0, false);
if (fragment.contains(suffix)) {
int afterIndex = StrUtil.indexOf(fragment, suffix, 0, false);
String between = fragment.substring(0, afterIndex);
if (between.length()>0)
if (between.length() > 0)
betweenList.add(between);
}
}
@ -2184,7 +2183,6 @@ public class StrUtil {
*
* @param str1 要比较的字符串1
* @param str2 要比较的字符串2
*
* @return 如果两个字符串相同或者都是<code>null</code>则返回<code>true</code>
*/
public static boolean equals(CharSequence str1, CharSequence str2) {
@ -2204,7 +2202,6 @@ public class StrUtil {
*
* @param str1 要比较的字符串1
* @param str2 要比较的字符串2
*
* @return 如果两个字符串相同或者都是<code>null</code>则返回<code>true</code>
*/
public static boolean equalsIgnoreCase(CharSequence str1, CharSequence str2) {
@ -2576,11 +2573,10 @@ public class StrUtil {
/**
* conjunction 为分隔符将多个对象转换为字符串
*
* @see ArrayUtil#join(Object, CharSequence)
*
* @param conjunction 分隔符
* @param objs 数组
* @return 连接后的字符串
* @see ArrayUtil#join(Object, CharSequence)
*/
public static String join(CharSequence conjunction, Object... objs) {
return ArrayUtil.join(objs, conjunction);
@ -3265,7 +3261,7 @@ public class StrUtil {
}
int len = str.length();
if (len < partLength) {
return new String[] { str.toString() };
return new String[]{str.toString()};
}
int part = NumberUtil.count(len, partLength);
final String[] array = new String[part];
@ -3605,7 +3601,7 @@ public class StrUtil {
* <p>
* 如果 str=null searchStr=null ordinal&ge;0 则返回-1<br>
* 此方法来自Apache-Commons-Lang
*
* <p>
* 例子*代表任意字符
*
* <pre>
@ -3648,6 +3644,7 @@ public class StrUtil {
}
// ------------------------------------------------------------------------------------------------------------------ Append and prepend
/**
* 如果给定字符串不是以给定的一个或多个字符串为结尾则在尾部添加结尾字符串<br>
* 不忽略大小写
@ -3655,7 +3652,6 @@ public class StrUtil {
* @param str 被检查的字符串
* @param suffix 需要添加到结尾的字符串
* @param suffixes 需要额外检查的结尾字符串如果以这些中的一个为结尾则不再添加
*
* @return 如果已经结尾返回原字符串否则返回添加结尾的字符串
* @since 3.0.7
*/
@ -3670,7 +3666,6 @@ public class StrUtil {
* @param str 被检查的字符串
* @param suffix 需要添加到结尾的字符串
* @param suffixes 需要额外检查的结尾字符串如果以这些中的一个为结尾则不再添加
*
* @return 如果已经结尾返回原字符串否则返回添加结尾的字符串
* @since 3.0.7
*/
@ -3685,7 +3680,6 @@ public class StrUtil {
* @param suffix 需要添加到结尾的字符串
* @param ignoreCase 检查结尾时是否忽略大小写
* @param suffixes 需要额外检查的结尾字符串如果以这些中的一个为结尾则不再添加
*
* @return 如果已经结尾返回原字符串否则返回添加结尾的字符串
* @since 3.0.7
*/
@ -3710,7 +3704,6 @@ public class StrUtil {
* @param str 被检查的字符串
* @param prefix 需要添加到首部的字符串
* @param prefixes 需要额外检查的首部字符串如果以这些中的一个为起始则不再添加
*
* @return 如果已经结尾返回原字符串否则返回添加结尾的字符串
* @since 3.0.7
*/
@ -3725,7 +3718,6 @@ public class StrUtil {
* @param str 被检查的字符串
* @param prefix 需要添加到首部的字符串
* @param prefixes 需要额外检查的首部字符串如果以这些中的一个为起始则不再添加
*
* @return 如果已经结尾返回原字符串否则返回添加结尾的字符串
* @since 3.0.7
*/
@ -3740,7 +3732,6 @@ public class StrUtil {
* @param prefix 需要添加到首部的字符串
* @param ignoreCase 检查结尾时是否忽略大小写
* @param prefixes 需要额外检查的首部字符串如果以这些中的一个为起始则不再添加
*
* @return 如果已经结尾返回原字符串否则返回添加结尾的字符串
* @since 3.0.7
*/
@ -4173,8 +4164,8 @@ public class StrUtil {
* 生成随机UUID
*
* @return UUID字符串
* @since 4.0.10
* @see IdUtil#randomUUID()
* @since 4.0.10
*/
public static String uuid() {
return IdUtil.randomUUID();

View File

@ -424,8 +424,8 @@ public class StrUtilTest {
@Test
public void subBetweenAllTest() {
Assert.assertArrayEquals(new String[]{"yz","abc"},StrUtil.subBetweenAll("saho[yz]fdsadp[abc]a","\\[","\\]"));
Assert.assertArrayEquals(new String[]{"abc"}, StrUtil.subBetweenAll("saho[yzfdsadp[abc]a]","\\[","\\]"));
Assert.assertArrayEquals(new String[]{"yz","abc"},StrUtil.subBetweenAll("saho[yz]fdsadp[abc]a","[","]"));
Assert.assertArrayEquals(new String[]{"abc"}, StrUtil.subBetweenAll("saho[yzfdsadp[abc]a]","[","]"));
}
}