How to remove all whitespace of a string in Dart?

后端 未结 7 1958
半阙折子戏
半阙折子戏 2021-02-07 10:31

Using trim() to eliminate white space in Dart and it doesn\'t work. What am I doing wrong or is there an alternative?

       String product = \"COC         


        
相关标签:
7条回答
  • 2021-02-07 11:12

    You can try this:

    String product = "COCA COLA";
    
    print(product.split(" ").join(""));   // COCACOLA
    
    0 讨论(0)
  • 2021-02-07 11:16

    Use Trim Function

    String name = "Stack Overflow";
    print(name.trim());
    
    0 讨论(0)
  • 2021-02-07 11:20

    the Trim method just remove the leading and trailing. Use Regexp instide: Here is an example: Dart: Use regexp to remove whitespaces from string

    0 讨论(0)
  • 2021-02-07 11:22

    Try this

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

    Update:

    String name = '4 ever 1 k g @@ @';
    print(name.replaceAll(new RegExp(r"\s+"), ""));
    
    0 讨论(0)
  • 2021-02-07 11:25
    1. Using trim()

      trim() method is used to remove leading and trailing whitespaces. It doesn't mutate the original string. If there is no whitespace at the beginning or the end of the String, it will return the original value.

      print('   COCA COLA'.trim()); // Output: 'COCA COLA'
      print('COCA COLA     '.trim()); // Output: 'COCA COLA'
      print('   COCA COLA     '.trim()); // Output: 'COCA COLA'
      
    2. Using trimLeft() and trimRight()

      What if you want to trim at the beginning only but not at the end, or maybe the opposite way. You can use trimLeft for removing leading whitespaces only and trimRight which removes trailing whitespaces only.

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

      If the String can be null, you can consider using null-aware operator.

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

      The above code will return null instead of throwing NoSuchMethodError.

    3. Using regular expression (RegExp)

      If the original string contains multiple whitespaces and you want remove all whitespaces. Apply below solution

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

      Call like this

      print(replaceWhitespacesUsingRegex('One  Two   Three   Four', '')); // Output: 'OneTwoThreeFour'
      

    See More: https://www.woolha.com/tutorials/dart-trim-whitespaces-of-a-string-examples

    0 讨论(0)
  • 2021-02-07 11:26

    In case this is of any help to someone in the future, for convenience you can define an extension method to the String class:

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

    This can be called like product.removeWhiteSpace() I've used it in the past to create a helper method when sorting lists by a string whilst ignoring case and whitespace

    extension StringExtensions on String {
      String toSortable() {
        return this.toLowerCase().replaceAll(' ', '');
      }
    }
    
    0 讨论(0)
提交回复
热议问题