diff --git a/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt b/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt index 226e301af..ce588c5cd 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt @@ -9,6 +9,7 @@ import io.legado.app.help.BookHelp import io.legado.app.model.Debug import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.utils.NetworkUtils +import io.legado.app.utils.StringUtils.wordCountFormat import io.legado.app.utils.htmlFormat object BookInfo { @@ -51,7 +52,7 @@ object BookInfo { } Debug.log(bookSource.bookSourceUrl, "└${book.kind}") Debug.log(bookSource.bookSourceUrl, "┌获取字数") - analyzeRule.getString(infoRule.wordCount).let { + wordCountFormat(analyzeRule.getString(infoRule.wordCount)).let { if (it.isNotEmpty()) book.wordCount = it } Debug.log(bookSource.bookSourceUrl, "└${book.wordCount}") diff --git a/app/src/main/java/io/legado/app/model/webBook/BookList.kt b/app/src/main/java/io/legado/app/model/webBook/BookList.kt index bb90d5f5e..474ed653c 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookList.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookList.kt @@ -10,6 +10,7 @@ import io.legado.app.model.Debug import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.utils.NetworkUtils +import io.legado.app.utils.StringUtils.wordCountFormat import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.isActive @@ -152,7 +153,7 @@ object BookList { Debug.log(bookSource.bookSourceUrl, "└${searchBook.kind}") if (!scope.isActive) throw CancellationException() Debug.log(bookSource.bookSourceUrl, "┌获取字数") - searchBook.wordCount = analyzeRule.getString(wordCount) + searchBook.wordCount = wordCountFormat(analyzeRule.getString(wordCount)) Debug.log(bookSource.bookSourceUrl, "└${searchBook.wordCount}") if (!scope.isActive) throw CancellationException() Debug.log(bookSource.bookSourceUrl, "┌获取最新章节") @@ -212,7 +213,7 @@ object BookList { Debug.log(bookSource.bookSourceUrl, "└${searchBook.kind}", log) if (!scope.isActive) throw CancellationException() Debug.log(bookSource.bookSourceUrl, "┌获取字数", log) - searchBook.wordCount = analyzeRule.getString(ruleWordCount) + searchBook.wordCount = wordCountFormat(analyzeRule.getString(ruleWordCount)) Debug.log(bookSource.bookSourceUrl, "└${searchBook.wordCount}", log) if (!scope.isActive) throw CancellationException() Debug.log(bookSource.bookSourceUrl, "┌获取最新章节", log) diff --git a/app/src/main/java/io/legado/app/utils/StringUtils.kt b/app/src/main/java/io/legado/app/utils/StringUtils.kt index 76f076af3..e5b658c21 100644 --- a/app/src/main/java/io/legado/app/utils/StringUtils.kt +++ b/app/src/main/java/io/legado/app/utils/StringUtils.kt @@ -214,7 +214,7 @@ object StringUtils { fun stringToInt(str: String?): Int { if (str != null) { - val num = fullToHalf(str).replace("\\s".toRegex(), "") + val num = fullToHalf(str).replace("\\s+".toRegex(), "") return try { Integer.parseInt(num) } catch (e: Exception) { @@ -226,17 +226,35 @@ object StringUtils { } fun isContainNumber(company: String): Boolean { - val p = Pattern.compile("[0-9]") + val p = Pattern.compile("[0-9]+") val m = p.matcher(company) return m.find() } fun isNumeric(str: String): Boolean { - val pattern = Pattern.compile("[0-9]*") + val pattern = Pattern.compile("[0-9]+") val isNum = pattern.matcher(str) return isNum.matches() } + fun wordCountFormat(wc: String?): String { + if (wc == null) return "" + var wordsS = "" + if (isNumeric(wc)) { + val words: Int = wc.toInt() + if (words > 0) { + wordsS = words.toString() + "字" + if (words > 10000) { + val df = DecimalFormat("#.#") + wordsS = df.format(words * 1.0f / 10000f.toDouble()) + "万字" + } + } + } else { + wordsS = wc + } + return wordsS + } + // 移除字符串首尾空字符的高效方法(利用ASCII值判断,包括全角空格) fun trim(s: String): String { if (isEmpty(s)) return ""