/* * Copyright 2025 mingliqiye * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ProjectName mingli-utils * ModuleName mingli-utils.main * CurrentFile StringUtils.kt * LastUpdate 2025-09-17 21:09:10 * UpdateUser MingLiPro */ @file:JvmName("StringUtils") package com.mingliqiye.utils.string import com.mingliqiye.utils.logger.mingLiLoggerFactory val log = mingLiLoggerFactory.getLogger("StringUtils") val NULLISH_STRINGS = setOf("null", "NaN", "undefined", "None", "none") /** * 判断`字符串`是否为空 * * @param str 待判断的字符串 * @return `true`: 空 `false`: 非空 */ @JvmName("isEmpty") fun String?.isNullish(): Boolean { return this == null || this.isBlank() || this in NULLISH_STRINGS } /** * 格式化字符串,将字符串中的占位符{}替换为对应的参数值 * * `Kotlin`语言给我老老实实用`$`啊 * * @param str 需要格式化的字符串,包含{}占位符 \\{} 代表一个{} * @param args 要替换占位符的参数列表 * @return 格式化后的字符串 */ fun format(str: String, vararg args: Any?): String { var argIndex = 0 val result = StringBuilder() var lastIndex = 0 // 匹配所有非转义的 {} val pattern = Regex("(? = unicode.split("\\\\u".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() // 从索引1开始遍历,因为分割后的第一个元素是转义符前面的内容(可能为空) for (i in 1.. { return str.split(separator) } fun List.join(separator: String): String { return this.joinToString(separator) }