Reputation: 19444
How to set space after letter,
example:
String abc = 05:12AM;
String abc = 5:12PM;
String abc = 12:12AM;
String abc = 1:12PM;
I want to like this result: 05:12 AM
I tried this way, but not working.
String a = abc.replaceAll(RegExp('[a-zA-Z]'), '');
Upvotes: 1
Views: 2259
Reputation: 10963
If you are creating the values of those strings with some date format, you probably want to add the space when you are formatting those strings. For example, using intl
package, you could do this:
String abc = DateFormat('HH:mm a').format(DateTime.now()); // 05:12 AM
If you have no control over the creations of those strings and you know for sure that you always receive AM or PM at the end, you could do this:
String a = '${abc.substring(0, abc.length - 2)} ${abc.substring(abc.length - 2)}';
or
String a = '${abc.replaceRange(abc.length-2, abc.length-2, ' ')}';
If it's possible that those strings come without AM or PM, you could do this:
String a = abc.replaceFirstMapped(RegExp('AM|PM'), (m) => ' ${m.group(0)}');
Upvotes: 1
Reputation: 9903
String abc = '1:12PM';
String a = abc.replaceAllMapped(RegExp('[a-zA-Z]+'), (m) => ' ${m.group(0)}');
or maybe like this to ensure you're splitting number and characters:
String a = abc.replaceAllMapped(RegExp('(\\d)([a-zA-Z]+)'), (m) => '${m.group(1)} ${m.group(2)}');
There's also replaceFirstMapped method
Upvotes: 1
Reputation: 1419
Looks like you only want to move 2 characters to the right with adding space. First u need to get the last two character with subString, then remove last two characters with subString. Then use + to add them with space. Code :
String abc = '05:12AM';
String lastTwo = abc.substring(abc.length-2,abc.length);
String removedLastTwo = abc.substring(0,abc.length-2);
String a = removedLastTwo + ' ' + lastTwo;
print('lastTwo = ' + lastTwo);
print('removedLastTwo = ' + removedLastTwo);
print('a = ' + a);
Upvotes: 1