当前位置:网站首页>dart:字符串replace相关的方法解决替换字符
dart:字符串replace相关的方法解决替换字符
2022-06-30 15:43:00 【华为云】
关于String中的关于这方面的一些操作。
replaceAll
String replaceAll(pattern from,String replace)
replaceAll第一个参数是匹配符,可以是字符串也可以是正则表达式,第二个参数是要替换的字符串,返回值是一个新字符串。
,我在这给大家写个Demo,大家就知道了
void main() { var a = '大前端️之旅️'; var b = '大前端️'; print(a.replaceAll('️', '')); print(b.replaceAll(RegExp(r'(️)'), '')); }运行效果如下:

上面的学会了没
我们接着来
replaceAllMapped
String replaceAllMapped(Pattern from,String replace(Match match))replaceAllMapped第一个参数是匹配符,可以是字符串也可以是正则,第二个参数是一个函数,函数的参数捕获到的字符串片段,其实就是对捕获的字符串片段的一个映射,看函数名的mapped就能看的出来。
void main() { var a = '大前端️之旅️'; var b = '大前端️'; print(a.replaceAllMapped('️', (Match m) => '')); print(b.replaceAllMapped(RegExp(r'️'), (Match m) => '')); }
replaceFirst
String replaceFirst(Pattern from,String to,[int startIndex = 0])replaceFirst第一参数是匹配符,可以是字符串也可以是正则,第二个参数是要替换的字符串,第三个参数可选,默认是0,。这个方法如果匹配到了多个字符串片段,那么只会替换下标startIndex的那个捕获片段,如果下标比捕获的片段长度大,那么将会报错
void main() { var d = '大前端之旅'; print(d.replaceFirst(RegExp(r'\大'), 'jianguo', 0)); //1ccc3}
4.replaceFirstMapped
String replaceFirstMapped(Pattern from,String replace(Match match),[int startIndex = 0])replaceFirstMapped和replaceAllMapped类似,替换捕获到的字符串片段中startIdnex对应的那个片段,并通过第二个参数传的函数进行转换。
var e = 'asd'; print(e.replaceFirstMapped(RegExp(r'\w'), (Match m) => '${m[0]}${m[0]}', 2),);//asdd5.replaceRange
String replaceRange(int start,int end,String replacement)replaceRange第一个参数是开始下标,第二个参数是结束下标,第三个参数是要替换的字符串。替换的字符串包含开始下标,不包含结束下标,并且开始下标的数字要>=0,小于等于结束下标,结束下标的值要小于等于字符串长度,否则会报错。
var f = '123456789'; print(f.replaceRange(1, 3, 'replace')); //1replace456789以上就是我用到的replace相关的一些操作,
在项目里我也是用这行代码就解决了问题。

边栏推荐
- Create statement for Oracle export view
- KDD 2022 | how far are we from the general pre training recommendation model? Universal sequence representation learning model unisrec for recommender system
- BYD is more and more like Huawei?
- “低代码”在企业数字化转型中扮演着什么角色?
- Container common commands
- Specific steps for installing mysql8.0 on Windows system
- linux下修改mysql密码: ERROR 1396 (HY000): Operation ALTER USER failed for ‘root‘@‘localhost‘
- 比亚迪越来越像华为?
- Policy Center > Misrepresentation
- ADB devices cannot detect the problem of Xiaomi note 3
猜你喜欢
![[unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid](/img/c9/ff22a30a638b5d9743d39e22ead647.png)
[unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid

几百行代码实现一个 JSON 解析器

Simulate user login function
Mysql8.0 method and steps for enabling remote connection permission

【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例

Policy Center > Google Play‘s Target API Level Policy

Data governance Market: Yixin Huachen faces left, Huaao data faces right

Policy Center > Malware > Malware

Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)

Interview experience of service end test engineer
随机推荐
[unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
Asp.NetCore利用缓存使用AOP方式防止重复提交
How cloudxr promotes the future development of XR
Google Play 索引表
360数科、蚂蚁集团等入选中国信通院“业务安全推进计划”成员单位
LeCun指明下一代AI方向:自主机器智能
Open source STM32 USB-CAN project
Mysql事务/锁/日志总结
ASP. Send information in sinalr controller of net core
Unsupported major. minor version 52.0
[cve-2019-0193] - Apache Solr dataimport remote command execution analysis
Alibaba cloud OSS object storage cross domain settings
开源 STM32 USB-CAN项目
波导的种类
Is your light on? Before you start to solve a problem, you need to know what the "real problem" is
How to connect the Internet Reading Notes - Summary
MySQL master-slave configuration
mysql8报错:ERROR 1410 (42000): You are not allowed to create a user with GRANT解决办法
ASP. Net core Middleware
智慧风电:数字孪生 3D 风机智能设备运维