当前位置:网站首页>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),);//asdd
5.replaceRange
String replaceRange(int start,int end,String replacement)
replaceRange第一个参数是开始下标,第二个参数是结束下标,第三个参数是要替换的字符串。替换的字符串包含开始下标,不包含结束下标,并且开始下标的数字要>=0,小于等于结束下标,结束下标的值要小于等于字符串长度,否则会报错。
var f = '123456789'; print(f.replaceRange(1, 3, 'replace')); //1replace456789
以上就是我用到的replace相关的一些操作,
在项目里我也是用这行代码就解决了问题。
边栏推荐
- 几百行代码实现一个 JSON 解析器
- Google play index table
- Which direction should college students choose to find jobs after graduation?
- Unsupported major.minor version 52.0
- 技不压身,快速入门ETH智能合约开发,带你进入ETH世界
- Model system: Sword (1)
- Compulsory national standard for electronic cigarette GB 41700-2022 issued and implemented on October 1, 2022
- topic: Privacy, Deception and Device Abuse
- [unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
- Oculus quest2 | unity configures the oculus quest2 development environment and packages an application for real machine testing
猜你喜欢
How to browse mobile web pages on your computer
ASP. Net core Middleware
Which direction should college students choose to find jobs after graduation?
[附下载]渗透测试神器Nessus安装及使用
iMeta | 叶茂/时玉等综述环境微生物组中胞内与胞外基因的动态穿梭与生态功能...
Generating verification code with sring
[time series database incluxdb] code example for configuring incluxdb+ data visualization and simple operation with C under Windows Environment
大学生研究生毕业找工作,该选择哪个方向?
Visualization of provincial GDP with CSV metabase processing
Policy Center > Malware > Malware
随机推荐
Policy Center > Deceptive Behavior
MySQL8.0开启远程连接权限的方法步骤
互联网研发效能之去哪儿网(Qunar)核心领域DevOps落地实践
flink sql cdc 同步sqlserver 报错什么原因啊
ASP. Net core Middleware
ASP. Net core signalr series hub tutorial
How to get the preferential activities for stock account opening? Is online account opening safe?
Policy Center > Malware > Malware
In depth analysis of the core code of the gadgetinspector
Finally understand science! 200 pictures to appreciate the peak of human wisdom
ADB devices cannot detect the problem of Xiaomi note 3
Generating verification code with sring
Google play index table
波导的种类
Asp.NetCore利用缓存使用AOP方式防止重复提交
什么是XR扩展现实,XR云串流平台有哪些
Specific steps for installing mysql8.0 on Windows system
从第三次技术革命看企业应用三大开发趋势
[time series database incluxdb] code example for configuring incluxdb+ data visualization and simple operation with C under Windows Environment
[CVE-2019-0193] - Apache Solr DataImport 远程命令执行分析