Dart:使用regexp从字符串中删除空格 [英] Dart: Use regexp to remove whitespaces from string

查看:417
本文介绍了Dart:使用regexp从字符串中删除空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Dart和Regexp从字符串中删除所有空格。给定以下字符串: test test1 test2我想要得到: testtest1test2。我已经阅读了一些使用javascript的示例,但是它们似乎在Dart中不起作用。到目前为止,有一些尝试:

I am attempting to remove all white spaces from a string using Dart and Regexp. Given the following string: "test test1 test2" I would want to get: "testtest1test2". I have read some examples in javascript but they do not seem to work in Dart. These are some attempts so far:

print("test test1 test2".replaceAll(new RegExp(r"/^\s+|\s+$|\s+(?=\s)/g"), ""));
print("test test1 test2".replaceAll(new RegExp(r"/\s+\b|\b\s/ig"), ""));

此方法基于:用于删除空格的正则表达式

有人可以告诉我这是怎么回事。

Can someone advise where I am going wrong with this.

推荐答案

我认为这涉及更多基础:
textWithWhitespace.replaceAll(new RegExp(r \s + \ b | \b\s | \s | \b),)

I think this covers more bases: textWithWhitespace.replaceAll(new RegExp(r"\s+\b|\b\s|\s|\b"), "")

当前接受的答案不是为我工作。在所有空白的情况下,我的空白并未删除

The current accepted answer was not working for me. In the case where it was all whitespace, my whitespace was not removed

String whitespace = "    ";
print(whitespace.replaceAll(new RegExp(r"\s\b|\b\s"), "").length);
//length is 4 here

这篇关于Dart:使用regexp从字符串中删除空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆