|
|
@ -41,7 +41,9 @@ object StringUtils { |
|
|
|
return map |
|
|
|
return map |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//将时间转换成日期 |
|
|
|
/** |
|
|
|
|
|
|
|
* 将时间转换成日期 |
|
|
|
|
|
|
|
*/ |
|
|
|
fun dateConvert(time: Long, pattern: String): String { |
|
|
|
fun dateConvert(time: Long, pattern: String): String { |
|
|
|
val date = Date(time) |
|
|
|
val date = Date(time) |
|
|
|
|
|
|
|
|
|
|
@ -50,7 +52,9 @@ object StringUtils { |
|
|
|
return format.format(date) |
|
|
|
return format.format(date) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//将日期转换成昨天、今天、明天 |
|
|
|
/** |
|
|
|
|
|
|
|
* 将日期转换成昨天、今天、明天 |
|
|
|
|
|
|
|
*/ |
|
|
|
fun dateConvert(source: String, pattern: String): String { |
|
|
|
fun dateConvert(source: String, pattern: String): String { |
|
|
|
@SuppressLint("SimpleDateFormat") |
|
|
|
@SuppressLint("SimpleDateFormat") |
|
|
|
val format = SimpleDateFormat(pattern) |
|
|
|
val format = SimpleDateFormat(pattern) |
|
|
@ -69,12 +73,8 @@ object StringUtils { |
|
|
|
if (oldHour == 0) { |
|
|
|
if (oldHour == 0) { |
|
|
|
//比日期:昨天今天和明天 |
|
|
|
//比日期:昨天今天和明天 |
|
|
|
return when { |
|
|
|
return when { |
|
|
|
difDate == 0L -> { |
|
|
|
difDate == 0L -> "今天" |
|
|
|
"今天" |
|
|
|
difDate < DAY_OF_YESTERDAY -> "昨天" |
|
|
|
} |
|
|
|
|
|
|
|
difDate < DAY_OF_YESTERDAY -> { |
|
|
|
|
|
|
|
"昨天" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else -> { |
|
|
|
else -> { |
|
|
|
@SuppressLint("SimpleDateFormat") |
|
|
|
@SuppressLint("SimpleDateFormat") |
|
|
|
val convertFormat = SimpleDateFormat("yyyy-MM-dd") |
|
|
|
val convertFormat = SimpleDateFormat("yyyy-MM-dd") |
|
|
@ -97,10 +97,12 @@ object StringUtils { |
|
|
|
}.onFailure { |
|
|
|
}.onFailure { |
|
|
|
it.printOnDebug() |
|
|
|
it.printOnDebug() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return "" |
|
|
|
return "" |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 单位转换 |
|
|
|
|
|
|
|
*/ |
|
|
|
fun toSize(length: Long): String { |
|
|
|
fun toSize(length: Long): String { |
|
|
|
if (length <= 0) return "0" |
|
|
|
if (length <= 0) return "0" |
|
|
|
val units = arrayOf("b", "kb", "M", "G", "T") |
|
|
|
val units = arrayOf("b", "kb", "M", "G", "T") |
|
|
@ -112,6 +114,9 @@ object StringUtils { |
|
|
|
.format(length / 1024.0.pow(digitGroups.toDouble())) + " " + units[digitGroups] |
|
|
|
.format(length / 1024.0.pow(digitGroups.toDouble())) + " " + units[digitGroups] |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 首字母大写 |
|
|
|
|
|
|
|
*/ |
|
|
|
@SuppressLint("DefaultLocale") |
|
|
|
@SuppressLint("DefaultLocale") |
|
|
|
fun toFirstCapital(str: String): String { |
|
|
|
fun toFirstCapital(str: String): String { |
|
|
|
return str.substring(0, 1).uppercase(Locale.getDefault()) + str.substring(1) |
|
|
|
return str.substring(0, 1).uppercase(Locale.getDefault()) + str.substring(1) |
|
|
@ -140,7 +145,9 @@ object StringUtils { |
|
|
|
return String(c) |
|
|
|
return String(c) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//功能:字符串全角转换为半角 |
|
|
|
/** |
|
|
|
|
|
|
|
* 字符串全角转换为半角 |
|
|
|
|
|
|
|
*/ |
|
|
|
fun fullToHalf(input: String): String { |
|
|
|
fun fullToHalf(input: String): String { |
|
|
|
val c = input.toCharArray() |
|
|
|
val c = input.toCharArray() |
|
|
|
for (i in c.indices) { |
|
|
|
for (i in c.indices) { |
|
|
@ -157,6 +164,9 @@ object StringUtils { |
|
|
|
return String(c) |
|
|
|
return String(c) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 中文大写数字转数字 |
|
|
|
|
|
|
|
*/ |
|
|
|
fun chineseNumToInt(chNum: String): Int { |
|
|
|
fun chineseNumToInt(chNum: String): Int { |
|
|
|
var result = 0 |
|
|
|
var result = 0 |
|
|
|
var tmp = 0 |
|
|
|
var tmp = 0 |
|
|
@ -207,6 +217,9 @@ object StringUtils { |
|
|
|
}.getOrDefault(-1) |
|
|
|
}.getOrDefault(-1) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 字符串转数字 |
|
|
|
|
|
|
|
*/ |
|
|
|
fun stringToInt(str: String?): Int { |
|
|
|
fun stringToInt(str: String?): Int { |
|
|
|
if (str != null) { |
|
|
|
if (str != null) { |
|
|
|
val num = fullToHalf(str).replace("\\s+".toRegex(), "") |
|
|
|
val num = fullToHalf(str).replace("\\s+".toRegex(), "") |
|
|
@ -219,12 +232,18 @@ object StringUtils { |
|
|
|
return -1 |
|
|
|
return -1 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 是否包含数字 |
|
|
|
|
|
|
|
*/ |
|
|
|
fun isContainNumber(company: String): Boolean { |
|
|
|
fun isContainNumber(company: String): Boolean { |
|
|
|
val p = Pattern.compile("[0-9]+") |
|
|
|
val p = Pattern.compile("[0-9]+") |
|
|
|
val m = p.matcher(company) |
|
|
|
val m = p.matcher(company) |
|
|
|
return m.find() |
|
|
|
return m.find() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 是否数字 |
|
|
|
|
|
|
|
*/ |
|
|
|
fun isNumeric(str: String): Boolean { |
|
|
|
fun isNumeric(str: String): Boolean { |
|
|
|
val pattern = Pattern.compile("[0-9]+") |
|
|
|
val pattern = Pattern.compile("[0-9]+") |
|
|
|
val isNum = pattern.matcher(str) |
|
|
|
val isNum = pattern.matcher(str) |
|
|
@ -249,7 +268,9 @@ object StringUtils { |
|
|
|
return wordsS |
|
|
|
return wordsS |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 移除字符串首尾空字符的高效方法(利用ASCII值判断,包括全角空格) |
|
|
|
/** |
|
|
|
|
|
|
|
* 移除字符串首尾空字符的高效方法(利用ASCII值判断,包括全角空格) |
|
|
|
|
|
|
|
*/ |
|
|
|
fun trim(s: String): String { |
|
|
|
fun trim(s: String): String { |
|
|
|
if (isEmpty(s)) return "" |
|
|
|
if (isEmpty(s)) return "" |
|
|
|
var start = 0 |
|
|
|
var start = 0 |
|
|
@ -265,6 +286,9 @@ object StringUtils { |
|
|
|
return if (start > 0 || end < len) s.substring(start, end) else s |
|
|
|
return if (start > 0 || end < len) s.substring(start, end) else s |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 重复字符串 |
|
|
|
|
|
|
|
*/ |
|
|
|
fun repeat(str: String, n: Int): String { |
|
|
|
fun repeat(str: String, n: Int): String { |
|
|
|
val stringBuilder = StringBuilder() |
|
|
|
val stringBuilder = StringBuilder() |
|
|
|
for (i in 0 until n) { |
|
|
|
for (i in 0 until n) { |
|
|
@ -273,6 +297,9 @@ object StringUtils { |
|
|
|
return stringBuilder.toString() |
|
|
|
return stringBuilder.toString() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 移除UTF头 |
|
|
|
|
|
|
|
*/ |
|
|
|
fun removeUTFCharacters(data: String?): String? { |
|
|
|
fun removeUTFCharacters(data: String?): String? { |
|
|
|
if (data == null) return null |
|
|
|
if (data == null) return null |
|
|
|
val p = Pattern.compile("\\\\u(\\p{XDigit}{4})") |
|
|
|
val p = Pattern.compile("\\\\u(\\p{XDigit}{4})") |
|
|
@ -286,6 +313,17 @@ object StringUtils { |
|
|
|
return buf.toString() |
|
|
|
return buf.toString() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private val regexNeedEscaped = |
|
|
|
|
|
|
|
arrayOf("\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun escapeToRegex(text: String): String { |
|
|
|
|
|
|
|
var str = text |
|
|
|
|
|
|
|
regexNeedEscaped.forEach { |
|
|
|
|
|
|
|
str = str.replace(it, "\\" + it) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return str |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun byteToHexString(bytes: ByteArray?): String { |
|
|
|
fun byteToHexString(bytes: ByteArray?): String { |
|
|
|
if (bytes == null) return "" |
|
|
|
if (bytes == null) return "" |
|
|
|
val sb = StringBuilder(bytes.size * 2) |
|
|
|
val sb = StringBuilder(bytes.size * 2) |
|
|
|