Home > Software engineering >  kotlin \n adds an extra space in output
kotlin \n adds an extra space in output

Time:01-27

So i have a string text = "" and when i want to increment i use text = "something", but i need to make a break line because i will write that text a couple times repeated, but when i do text ="\n" it adds the spaces between the "something" texts but after that it adds another space that i dont want. enter image description here

enter image description here

CodePudding user response:

I would not do it iteratively. So instead of using a loop, you can use joinToString for that, like this:

lines.joinToString(separator = "\n")

CodePudding user response:

val result = string.substringBeforeLast("\n")

CodePudding user response:

This might help!

fun main() {
        var string=""
        for (line in 0 until 4){
            string  = "Something"
            if (line!=3){
                string ="\n"
            }
        }
        print(string)
    }
  •  Tags:  
  • Related