【发布时间】:2021-01-04 12:54:53
【问题描述】:
您好,我们的应用程序需要一个格式化字符串。
预期输出:
Blow the candles real hard,
Coz now you have aged,
Don't pretend so much my dear,
Don't behave so sage,
Happy birthday to you,
Have a nice day,
Make the most of your day!
我想格式化一个字符串,如上所示,当我对字符串使用 trim() 函数时,它给出的结果为
Blow the candles real hard,
Coz now you have aged,
Don't pretend so much my dear,
Don't behave so sage,
Happy birthday to you,
Have a nice day,
Make the most of your day!
我想删除每行开头的空格。我在下面使用了这些函数,它修剪了字符串的开头和结尾,而不是行。
poemText = poemText.trimMargin()
poemText = poemText.trimIndent()
poemText = poemText.replace("\n ", "\n")
for(line in poemText.lines())
{line.trim()}
感谢任何帮助
【问题讨论】:
-
我的错:我的第一个答案没有涵盖文本开头有空格的情况,也没有涵盖每行末尾的情况,我改变了它你可以使用新的解决方案。跨度>
标签: java android string kotlin text