【问题标题】:How to remove all whitespace of a string in Dart?如何删除 Dart 中字符串的所有空格?
【发布时间】:2019-01-01 15:34:05
【问题描述】:

使用trim() 消除 Dart 中的空白,但它不起作用。 我做错了什么还是有替代方法?

       String product = "COCA COLA";

       print('Product id is: ${product.trim()}');

控制台打印:Product id is: COCA COLA

【问题讨论】:

  • 对我来说是正确的。你希望它打印什么?
  • “COCA COLA”修剪的是“COCA COLA”,不是吗?
  • 结果应该是COCACOLA,没有空格
  • trim() 删除字符串开头和结尾的空格。它不会影响内部的空间。
  • 看string.replaceAll.

标签: string flutter dart whitespace


【解决方案1】:

var s = "Coca Cola"; s.replaceAll(' ','');

【讨论】:

  • 首先,s 在此运行后保持不变。其次,即使这确实有效,这是否真的增加了最重要的答案?
【解决方案2】:

有很多方法可以做到这一点。

最明显的一个:

String product = "COCA COLA";
print('Product id is: ${product.replaceAll(' ', '')}');  // COCACOLA

普通函数:

String removeAllWhitespaces(String string) {
  return string.replaceAll(' ', '');
}

String product = "COCA COLA";
print('Product id is: ${removeAllWhitespaces(product)}');  // COCACOLA

带扩展方法:

extension StringRemoveWhitespace on String { 
String get removeAllWhitespaces => replaceAll(' ', '');  // COCACOLA
}

String product = "COCA COLA";
print('Product id is: ${product.removeAllWhitespaces}');

【讨论】:

    【解决方案3】:

    试试这个

    String product = "COCA COLA";
    print('Product id is: ${product.replaceAll(new RegExp(r"\s+\b|\b\s"), "")}');
    

    更新:

    String name = '4 ever 1 k g @@ @';
    print(name.replaceAll(RegExp(r"\s+"), ""));
    

    另一个简单的解决方案:

    String name = '4 ever 1 k g @@ @';
    print(name.replaceAll(' ', '');
    

    【讨论】:

    • 别担心,这是一种享受
    • 这不行..试试这个String name = '4 ever 1 k g @@ @'; print(name.replaceAll(new RegExp(r"\s+\b|\b\s"), ""));
    • @Fobos 空白未删除,请分享其他正则表达式
    • @FurkanVijapura 试试这个:字符串名称 = '4 ever 1 k g @@ @'; print(name.replaceAll(new RegExp(r"\s+"), ""));
    • 所以.. 应该与更新一去?有什么区别?
    【解决方案4】:

    我知道这个问题有很好的答案,但我想展示一种去除字符串中所有空格的奇特方法。我实际上认为 Dart 应该有一个内置的方法来处理这个问题,所以我为 String 类创建了以下扩展:

    extension ExtendedString on String {
      /// The string without any whitespace.
      String removeAllWhitespace() {
        // Remove all white space.
        return this.replaceAll(RegExp(r"\s+"), "");
      }
    }
    

    现在,您可以以非常简单整洁的方式使用它:

    String product = "COCA COLA";
    print('Product id is: ${product.removeAllWhitespace()}');
    

    【讨论】:

      【解决方案5】:
      1. 使用正则表达式 (RegExp)

        如果原始字符串包含多个空格并且您想要删除所有空格。应用以下解决方案

        String replaceWhitespacesUsingRegex(String s, String replace) {
         if (s == null) {
           return null;
         }
        
         // This pattern means "at least one space, or more"
         // \\s : space
         // +   : one or more
         final pattern = RegExp('\\s+');
         return s.replaceAll(pattern, replace);
        }
        

        这样打电话

        print(replaceWhitespacesUsingRegex('One  Two   Three   Four', '')); // Output: 'OneTwoThreeFour'
        
      2. 使用 trim()

        trim() 方法用于删除前导和尾随空格。它不会改变原始字符串。如果String结尾的开头没有空格,则返回原值。

        print('   COCA COLA'.trim()); // Output: 'COCA COLA'
        print('COCA COLA     '.trim()); // Output: 'COCA COLA'
        print('   COCA COLA     '.trim()); // Output: 'COCA COLA'
        
      3. 使用trimLeft()trimRight()

        如果您只想在开头修剪而不在结尾修剪,或者可能相反。您可以使用trimLeft 仅删除前导空格,使用trimRight 仅删除尾随空格。

        print('   COCA COLA    '.trimLeft()); // Output: 'COCA COLA     '
        print('   COCA COLA    '.trimRight()); // Output:'   COCA COLA'
        

        如果String可以是null,可以考虑使用null-aware运算符。

        String s = null;
        print(s?.trim());
        

        以上代码将返回null,而不是抛出NoSuchMethodError

      这个答案来自和作者:https://www.woolha.com/tutorials/dart-trim-whitespaces-of-a-string-examples

      【讨论】:

      • 你知道这个“+$”是什么意思吗?
      【解决方案6】:

      这会解决你的问题

      String name = "COCA COLA";
      print(name.replaceAll(' ', ''));
      

      【讨论】:

      • 如何去除同一个字符串中出现的不同?就像我的字符串是 "hello%25sachin%20here" 一样,我想同时删除 '%25' 和 '%20" ......
      【解决方案7】:

      你可以试试这个:

      String product = "COCA COLA";
      
      print(product.split(" ").join(""));   // COCACOLA
      

      【讨论】:

        【解决方案8】:

        如果这对将来的某人有任何帮助,为方便起见,您可以为 String 类定义一个扩展方法:

        extension StringExtensions on String {
          String removeWhitespace() {
            return this.replaceAll(' ', '');
          }
        }
        

        这可以像product.removeWhiteSpace() 一样被调用,我过去用它来创建一个帮助方法,在按字符串排序列表时忽略大小写和空格

        extension StringExtensions on String {
          String toSortable() {
            return this.toLowerCase().replaceAll(' ', '');
          }
        }
        

        【讨论】:

          【解决方案9】:

          使用修剪功能

          String name = "Stack Overflow";
          print(name.trim());
          

          【讨论】:

          • trim 只是删除字符串开头和结尾的空格
          • 错误答案。我对此投了反对票,因为问题是关于删除所有空格,包括中间空格。 trim() 只删除前导和尾随空格。
          【解决方案10】:

          Trim 方法只是删除前导和尾随。在里面使用正则表达式: 这是一个例子: Dart: Use regexp to remove whitespaces from string

          【讨论】:

            猜你喜欢
            • 2011-08-24
            • 2023-04-08
            • 2012-01-06
            • 2019-11-25
            • 1970-01-01
            相关资源
            最近更新 更多