Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting camelCase strings to underscore in dart language

in java 9 we can simply convert camelCase to underscore like camel_case

String text = "camelCase";
Matcher m = Pattern.compile("(?<=[a-z])[A-Z]").matcher(text);
String result = m.replaceAll(match -> "_" + match.group().toLowerCase());

now my question is whats equivalent of this code in dart?

like image 301
DolDurma Avatar asked Apr 13 '26 13:04

DolDurma


1 Answers

You can use RegExp and the method replaceAllMapped:

String text = 'camelCase';
final exp = RegExp('(?<=[a-z])[A-Z]');
String result = text.replaceAllMapped(exp, (m) => '_${m.group(0)}').toLowerCase();
like image 146
Daniele Paggi Avatar answered Apr 16 '26 04:04

Daniele Paggi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!