代码之家  ›  专栏  ›  技术社区  ›  braulio.cassule

如何删除省道中字符串的所有空白?

  •  0
  • braulio.cassule  · 技术社区  · 8 年前

    使用 trim() 以消除省道中的空白,但它不起作用。 我做错了什么?还是有别的选择?

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

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

    2 回复  |  直到 7 年前
        1
  •  7
  •   Fobos    8 年前

    试试这个

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

    修剪方法只是删除前导和尾随。使用Regexp institde: 下面是一个例子: Dart: Use regexp to remove whitespaces from string

        3
  •  2
  •   SilenceCodder    7 年前

    使用微调功能

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