格式化字数

pull/315/head
Knock 4 years ago
parent c300f6b9e1
commit 4b66d33e35
  1. 3
      app/src/main/java/io/legado/app/model/webBook/BookInfo.kt
  2. 5
      app/src/main/java/io/legado/app/model/webBook/BookList.kt
  3. 24
      app/src/main/java/io/legado/app/utils/StringUtils.kt

@ -9,6 +9,7 @@ import io.legado.app.help.BookHelp
import io.legado.app.model.Debug import io.legado.app.model.Debug
import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.StringUtils.wordCountFormat
import io.legado.app.utils.htmlFormat import io.legado.app.utils.htmlFormat
object BookInfo { object BookInfo {
@ -51,7 +52,7 @@ object BookInfo {
} }
Debug.log(bookSource.bookSourceUrl, "${book.kind}") Debug.log(bookSource.bookSourceUrl, "${book.kind}")
Debug.log(bookSource.bookSourceUrl, "┌获取字数") Debug.log(bookSource.bookSourceUrl, "┌获取字数")
analyzeRule.getString(infoRule.wordCount).let { wordCountFormat(analyzeRule.getString(infoRule.wordCount)).let {
if (it.isNotEmpty()) book.wordCount = it if (it.isNotEmpty()) book.wordCount = it
} }
Debug.log(bookSource.bookSourceUrl, "${book.wordCount}") Debug.log(bookSource.bookSourceUrl, "${book.wordCount}")

@ -10,6 +10,7 @@ import io.legado.app.model.Debug
import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.StringUtils.wordCountFormat
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
@ -152,7 +153,7 @@ object BookList {
Debug.log(bookSource.bookSourceUrl, "${searchBook.kind}") Debug.log(bookSource.bookSourceUrl, "${searchBook.kind}")
if (!scope.isActive) throw CancellationException() if (!scope.isActive) throw CancellationException()
Debug.log(bookSource.bookSourceUrl, "┌获取字数") Debug.log(bookSource.bookSourceUrl, "┌获取字数")
searchBook.wordCount = analyzeRule.getString(wordCount) searchBook.wordCount = wordCountFormat(analyzeRule.getString(wordCount))
Debug.log(bookSource.bookSourceUrl, "${searchBook.wordCount}") Debug.log(bookSource.bookSourceUrl, "${searchBook.wordCount}")
if (!scope.isActive) throw CancellationException() if (!scope.isActive) throw CancellationException()
Debug.log(bookSource.bookSourceUrl, "┌获取最新章节") Debug.log(bookSource.bookSourceUrl, "┌获取最新章节")
@ -212,7 +213,7 @@ object BookList {
Debug.log(bookSource.bookSourceUrl, "${searchBook.kind}", log) Debug.log(bookSource.bookSourceUrl, "${searchBook.kind}", log)
if (!scope.isActive) throw CancellationException() if (!scope.isActive) throw CancellationException()
Debug.log(bookSource.bookSourceUrl, "┌获取字数", log) Debug.log(bookSource.bookSourceUrl, "┌获取字数", log)
searchBook.wordCount = analyzeRule.getString(ruleWordCount) searchBook.wordCount = wordCountFormat(analyzeRule.getString(ruleWordCount))
Debug.log(bookSource.bookSourceUrl, "${searchBook.wordCount}", log) Debug.log(bookSource.bookSourceUrl, "${searchBook.wordCount}", log)
if (!scope.isActive) throw CancellationException() if (!scope.isActive) throw CancellationException()
Debug.log(bookSource.bookSourceUrl, "┌获取最新章节", log) Debug.log(bookSource.bookSourceUrl, "┌获取最新章节", log)

@ -214,7 +214,7 @@ object StringUtils {
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(), "")
return try { return try {
Integer.parseInt(num) Integer.parseInt(num)
} catch (e: Exception) { } catch (e: Exception) {
@ -226,17 +226,35 @@ object StringUtils {
} }
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)
return isNum.matches() 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值判断,包括全角空格) // 移除字符串首尾空字符的高效方法(利用ASCII值判断,包括全角空格)
fun trim(s: String): String { fun trim(s: String): String {
if (isEmpty(s)) return "" if (isEmpty(s)) return ""

Loading…
Cancel
Save