How to remove all whitespace of a string in Dart?

后端 未结 7 1955
半阙折子戏
半阙折子戏 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: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(' ', '');
      }
    }
    

提交回复
热议问题