当前位置:网站首页>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相关的一些操作,
在项目里我也是用这行代码就解决了问题。

边栏推荐
- Distributed machine learning: model average Ma and elastic average easgd (pyspark)
- CloudXR如何推动XR的未来发展
- Summary of gradient descent optimizer (rmsprop, momentum, Adam)
- Is your light on? Before you start to solve a problem, you need to know what the "real problem" is
- KDD 2022 | how far are we from the general pre training recommendation model? Universal sequence representation learning model unisrec for recommender system
- Create a new MySQL database under Linux and import SQL files
- Practical cases of data visualization (timeline rotation diagram, streamlit control year metabase visualization tutorial) 2.0
- 【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例
- Policy Center-Permissions and APIs that Access Sensitive Information
- Unsupported major. minor version 52.0
猜你喜欢

实时渲染和预渲染有什么区别

数据治理市场:亿信华辰朝左,华傲数据向右

Under the pressure of technology, you can quickly get started with eth smart contract development, which will take you into the ETH world

Google play index table

智慧风电:数字孪生 3D 风机智能设备运维

ASP. Net core Middleware

What is the difference between real-time rendering and pre rendering
MySQL开放远程连接权限的两种方法

Policy Center > Deceptive Behavior

Is your light on? Before you start to solve a problem, you need to know what the "real problem" is
随机推荐
Go micro installation
Policy Center > Malware > Malware
超 Nice 的表格响应式布局小技巧
Container common commands
With as subquery in Oracle
String common API
【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例
LeCun指明下一代AI方向:自主机器智能
Warning: [antd: Menu] `children` will be removed in next major version. Please use `items` instead.
360数科、蚂蚁集团等入选中国信通院“业务安全推进计划”成员单位
Solution for IIS failing to load font files (*.woff, *.svg)
Implementation of Devops in the core field of qunar, the Internet R & D Efficiency
Which direction should college students choose to find jobs after graduation?
What is XR extended reality and what are the XR cloud streaming platforms
【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例
Warning: [antd: Menu] `children` will be removed in next major version. Please use `items` instead.
[cve-2019-0193] - Apache Solr dataimport remote command execution analysis
优惠券种类那么多,先区分清楚再薅羊毛!
topic: Privacy, Deception and Device Abuse
Policy Center > Malware > Malware