Home > Mobile >  Kotlin - Add new line after four commas in string
Kotlin - Add new line after four commas in string

Time:08-11


Since I'm not able to find a solution to this problem I'm going to post here a question, hoping someone will help me.

I have a string like this "One, Two, Three, Four, Five, Six, ..." and I need to find a way to insert a new line (\n) after the fourth comma, in this case "One, Two, Three, Four, \n Five, Six, ...".

And this is the code I'm trying to use

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

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

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


UPDATE:

This is my code now, no errors, no warnings but the output is now like this "[Ljava.lang.String@1c7172b" instead of the ingredients, what am I missing now ?

UPDATE 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
                    }
                }
            }


Thanks in advance to everyone that will try to help me.

CodePudding user response:

You could do this

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

CodePudding user response:

couple of issues with your code:

  • replace returns new string
  • var commasCount = 0 should be outside loop
  • commasCount should be reset once reached 4

try this out:

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 will be:

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

UPDATE: convert it to array after adding new line

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
                    }
                }
            }

CodePudding user response:

try

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