当前位置:网站首页>Dart: string replace related methods to solve replacement characters

Dart: string replace related methods to solve replacement characters

2022-06-30 16:18:00 Hua Weiyun

Today we have such a problem , There will be several special characters in the text display ,"️", This is because I inserted it while editing , It is easy to distinguish whether it is a picture , But in the process of text display, there are , So is there a way for him not to show , The answer is yes , Next, let's take a look at

About String Some operations on this aspect in .

replaceAll

String replaceAll(pattern from,String replace)

replaceAll The first parameter is the matcher , It can be a string or a regular expression , The second parameter is the string to replace , The return value is a new string .

, I'm here to write a Demo, As you all know

​void main() {​  var a = ' Big front end ️ The journey ️';  var b = ' Big front end ️';  print(a.replaceAll('️', ''));   print(b.replaceAll(RegExp(r'(️)'), '')); ​​​}​


The operation effect is as follows :

image-20220630110959918



Have you learned the above

Let's go on


replaceAllMapped

String replaceAllMapped(​Pattern from,​String replace(Match match)​)

replaceAllMapped The first parameter is the matcher , It can be a string or a regular , The second argument is a function , The string fragment captured by the parameter of the function , In fact, it is a mapping of the captured string fragments , Look at the function name mapped You can see .

​void main() {​  var a = ' Big front end ️ The journey ️';  var b = ' Big front end ️';​  print(a.replaceAllMapped('️', (Match m) => ''));  print(b.replaceAllMapped(RegExp(r'️'), (Match m) => '')); ​​​}​


image-20220630131739894


replaceFirst

String replaceFirst(​Pattern from,​String to,​[int startIndex = 0]​)

replaceFirst The first parameter is the match character , It can be a string or a regular , The second parameter is the string to replace , The third parameter is optional , The default is 0,. If this method matches multiple string fragments , Then it will only replace the subscript startIndex The capture of , If the subscript is larger than the length of the captured fragment , Then there will be an error

​void main() {​    var d = ' Big front end tour ';  print(d.replaceFirst(RegExp(r'\ Big '), 'jianguo', 0)); //1ccc3}​

image-20220630132818538



4.replaceFirstMapped

String replaceFirstMapped(​Pattern from,​String replace(Match match),​[int startIndex = 0]​)

replaceFirstMapped and replaceAllMapped similar , Replace the captured string fragment startIdnex The corresponding segment , And through the function passed by the second parameter .

  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 The first parameter is the starting subscript , The second parameter is the ending subscript , The third parameter is the string to replace . The replaced string contains the starting subscript , Does not include end subscript , And start subscribing numbers to >=0, Less than or equal to the end subscript , The value of the end subscript must be less than or equal to the length of the string , Otherwise, an error will be reported .

​​  var f = '123456789';  print(f.replaceRange(1, 3, 'replace')); //1replace456789

The above is what I use replace Some related operations ,

In the project, I also used this line of code to solve the problem .

image-20220630133117351

The same is true of practical work , And a process of solving problems , As long as we insist on , There will be gains .

原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/181/202206301407189891.html