簡體   English   中英

在字符串中的四個逗號后添加新行

[英]Add new line after four commas in string


由於我無法找到解決此問題的方法,因此我將在這里發布一個問題,希望有人能幫助我。

我有一個像這樣的字符串"One, Two, Three, Four, Five, Six, ..." ,我需要找到一種方法在第四個逗號之后插入一個新行(\ n),在這種情況下是"One, Two, Three, Four, \n Five, Six, ..."

這是我正在嘗試使用的代碼

for (letters in ingredients.toString()) {
     var commasCount = 0

     if (letters.toString() == ",") {
         commasCount++

         if (commasCount == 4) {
            ingredients.toString().replace(",", "\n")
         }
     }
}


更新:

這是我現在的代碼,沒有錯誤,沒有警告,但是 output 現在是這樣的“[Ljava.lang.String@1c7172b”而不是成分,我現在缺少什么?

更新 PT.2:

database.collection("Pizze").whereEqualTo("section", sectionNumber).get()
            .addOnCompleteListener {result ->
                if (result.isSuccessful) {
                    for (document in result.result) {
                        val name = document.data["_name"].toString()
                        val price = document.data["price"].toString()
                        val section = document.data["section"].toString()

                        var ingredients = arrayOf(document.data["ingredients"].toString()
                            .replace("[", "")
                            .replace("]", "")).toString()

                        var commasCount = 0
                        for (letters in ingredients.indices) {

                            if (ingredients[letters].toString() == ",") {
                                commasCount++

                                if (commasCount == 4) {
                                    commasCount = 0
                                    ingredients = arrayOf(ingredients.replaceRange(letters, letters + 2, ", \\n ")).toString()
                                }
                            }
                        }

                        data.add(PizzaViewModel(name, price, section, arrayOf(ingredients)))
                        data.sortBy { it.name }
                        val adapter = PizzaCustomAdapter(data, baseContext)
                        recyclerView.adapter = adapter
                    }
                }
            }


在此先感謝所有願意幫助我的人。

你可以這樣做

fun main() {
    val string = "One, Two, Three, Four, Five, Six, Seven, Eight, Nine"
    val result = string.split(", ")
        .chunked(4)
        .joinToString("\n") { it.joinToString(", ") }
    println(result)
}

Output:

One, Two, Three, Four
Five, Six, Seven, Eight
Nine

您的代碼有幾個問題:

  • replace返回新字符串
  • var commasCount = 0應該在循環之外
  • commasCount應在達到 4 后重置

試試這個:

var ingredients = "One, Two, Three, Four, Five, Six, Two, Three, Four, Five, Six"
var commasCount = 0
for (letters in ingredients.indices) {

    if (ingredients[letters].toString() == ",") {
        commasCount++

        if (commasCount == 4) {
            commasCount = 0
            ingredients = ingredients.replaceRange(letters, letters + 2, ", \n")
        }
    }
}

Output 將是:

One, Two, Three, Four,
Five, Six, Two, Three
Four, Five, Six

更新:添加新行后將其轉換為數組

database.collection("Pizze").whereEqualTo("section", sectionNumber).get()
            .addOnCompleteListener {result ->
                if (result.isSuccessful) {
                    for (document in result.result) {
                        val name = document.data["_name"].toString()
                        val price = document.data["price"].toString()
                        val section = document.data["section"].toString()

                        var ingredients = document.data["ingredients"].toString()
                            .replace("[", "")
                            .replace("]", "")

                        var commasCount = 0
                        for (letters in ingredients.indices) {

                            if (ingredients[letters].toString() == ",") {
                                commasCount++

                                if (commasCount == 4) {
                                    commasCount = 0
                                    ingredients = ingredients.replaceRange(letters, letters + 2, ", \n")
                                }
                            }
                        }

                        data.add(PizzaViewModel(name, price, section, arrayOf(ingredients)))
                        data.sortBy { it.name }
                        val adapter = PizzaCustomAdapter(data, baseContext)
                        recyclerView.adapter = adapter
                    }
                }
            }

嘗試

ingredients.toString().replace(",", "\\n")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM