【问题标题】:How to trim multiple lines in a string Android?如何修剪字符串Android中的多行?
【发布时间】: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


【解决方案1】:

您可以像这样使用正则表达式替换:

poemText = poemText.replace("""^\s*|\s*$""".toRegex(), "")
                   .replace("""\s*(\r|\n|\r\n)\s*""".toRegex(), "\n")  

Code example

第一次替换将删除文本开头和结尾的所有空格,第二次将删除每行末尾和开头的空格。

【讨论】:

    【解决方案2】:

    您可以使用 ^\h+ 匹配字符串开头的 1 个或多个水平空白字符,并使用 (?m) 启用多行。

    在替换中使用空字符串。

         val poemText = """
    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!"""
        
        println(
            """(?m)^\h+"""
            .toRegex()
            .replace(poemText, "")
        )
    

    输出

    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!
    

    查看Kotlin demo

    【讨论】:

      【解决方案3】:

      我觉得你可以把java变成kotlin

       String text = "Blow the candles real hard,\n " +
                  "    Coz now you have aged,\n " +
                  "    Don't pretend so much my dear,\n " +
                  "    Don't behave so sage,\n " +
                  "    Happy birthday to you,\n " +
                  "    Have a nice day,\n " +
                  "    Make the most of your day!";
      
          String[] lines = null;
          lines = text.split("\n");
      
          String newText = "";
          for(int i = 0 ; i<lines.length ; i++){
              newText = (newText + lines[i]).trim()+"\n";
          }
      
          Log.e("newText",newText);
      

      【讨论】:

      • 感谢您的回答,但对于这项任务来说,这似乎是一个过度的操作:)
      【解决方案4】:

      这有帮助吗?

      poemText = poemText.trim().replaceAll("\n ", "\n")
      

      因为句子之间有空格所以首先 trim() 去掉字符串的开头和结尾,然后用\n替换逗号和中间的空格

      【讨论】:

      • 这将用新行更改全空白。它没有帮助
      • 我的错,编辑了答案。当然,如果您在所有行之间都有,
      • 没有帮助 :(,不是所有的诗都以“,”结尾
      • 刚刚在 IDE 中运行了我的解决方案,它也可以工作。
      • 感谢您的努力,但对我没有用
      猜你喜欢
      • 2012-12-21
      • 1970-01-01
      • 2017-10-16
      • 2022-01-01
      • 1970-01-01
      • 2022-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多