浏览代码

添加一些

tuonina 5 年之前
父节点
当前提交
52bda5fdfc
共有 1 个文件被更改,包括 61 次插入0 次删除
  1. 61 0
      tuon-core/src/main/java/cn/tonyandmoney/tuon/core/utils/CoreUtils.java

+ 61 - 0
tuon-core/src/main/java/cn/tonyandmoney/tuon/core/utils/CoreUtils.java

@@ -0,0 +1,61 @@
+package cn.tonyandmoney.tuon.core.utils;
+
+import cn.tonyandmoney.tuon.core.TuonCoreKt;
+import org.springframework.lang.Nullable;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import static org.springframework.util.StringUtils.deleteAny;
+
+/**
+ * 工具类
+ */
+public class CoreUtils {
+
+
+    public static List<String> delimitedList(String str) {
+        return delimitedList(str, TuonCoreKt.DELIMITER);
+    }
+
+    public static List<String> delimitedList(@Nullable String str, @Nullable String delimiter) {
+        return delimitedList(str, delimiter, null);
+    }
+
+    /**
+     * 将字符串转换成数组
+     *
+     * @param str           原始数据
+     * @param delimiter     分隔符
+     * @param charsToDelete
+     * @return
+     */
+    public static List<String> delimitedList(@Nullable String str, @Nullable String delimiter, @Nullable String charsToDelete) {
+        if (str == null) {
+            return Collections.emptyList();
+        }
+        if (delimiter == null) {
+            return Collections.emptyList();
+        }
+
+        List<String> result = new ArrayList<>();
+        if (delimiter.isEmpty()) {
+            for (int i = 0; i < str.length(); i++) {
+                result.add(deleteAny(str.substring(i, i + 1), charsToDelete));
+            }
+        } else {
+            int pos = 0;
+            int delPos;
+            while ((delPos = str.indexOf(delimiter, pos)) != -1) {
+                result.add(deleteAny(str.substring(pos, delPos), charsToDelete));
+                pos = delPos + delimiter.length();
+            }
+            if (str.length() > 0 && pos <= str.length()) {
+                // Add rest of String, but not in case of empty input.
+                result.add(deleteAny(str.substring(pos), charsToDelete));
+            }
+        }
+        return result;
+    }
+}