I found methods to convert normal string to camel case and snake case in dart but I want to implement SnakeCase to normal sentence.
for eg: myNameIsJohnDoe to My name is john doe
I found methods to convert normal string to camel case and snake case in dart but I want to implement SnakeCase to normal sentence.
for eg: myNameIsJohnDoe to My name is john doe
There are couple of ways to do it :
import 'package:recase/recase.dart';
void main() {
print("my_name_is_john_doe".sentenceCase); //snake_case to normal sentence;
print("myNameIsJohnDoe".sentenceCase); //camelCase to normal Sentence;
print("my_name_is_john_doe".snakeCasetoSentenceCase()); //Other way of converting snake case to Normal Sentence
}
extension StringExtension on String {
String snakeCasetoSentenceCase() {
return "${this[0].toUpperCase()}${this.substring(1)}"
.replaceAll(RegExp(r'(_|-)+'), ' ');
}
}
Output:
My name is john doe
My name is john doe
My name is john doe
Flutter Only Solution , as get does not support pure dart
You can try this :
String text = 'myNameIsJohnDoe';
RegExp exp = RegExp(r'(?<=[a-z])[A-Z]');
String result = text.replaceAllMapped(exp, (Match m) => (' ' + m.group(0))).capitalizeFirst;
print('Result : $result');
Output :
Result : My name is John doe
#UPDATE :
Installing get to use capitalizeFirst
dependencies:
get: ^4.1.4
Import it , Now in your Dart code, you can use:
import 'package:get/get.dart';