I want space between the numbers and letters in a string. example: String = "KL11AB2432"; to String = "KL 11 AB 2432";
4 Answers
Y'all are working too hard. Reach for a regex!
void main(List<String> arguments) {
const data = "KL11AB2432";
print(insertSpaces(data));
}
String insertSpaces(String input) {
final reg = RegExp(r"([A-Z]+|\d+)");
final pieces = reg.allMatches(input);
return pieces.map((e) => e.group(0)).join(" ");
}
Comments
String str = "KL11AB2432";
List<Object> newList = [];
List<String> strList = str.split("").toList();
for (var i = 0; i < strList.length; i++) {
if (int.tryParse(strList[i]) != null) {
if ((i - 1) >= 0 && int.tryParse(strList[i - 1]) == null) {
newList.add(" ");
newList.add(strList[i]);
} else {
newList.add(strList[i]);
}
} else {
if ((i - 1) >= 0 && int.tryParse(strList[i - 1]) != null) {
newList.add(" ");
newList.add(strList[i]);
} else {
newList.add(strList[i]);
}
}
}
print(newList);
print(newList.join().toString());
and Result =>
[K, L, , 1, 1, , A, B, , 2, 4, 3, 2]
KL 11 AB 2432
Comments
I am using try catch to check the int type,
bool isInt(String data) {
try {
int.parse(data);
return true;
} catch (_) {
return false;
}
}
void main() {
String data = "KL11AB2432";
bool isPrevIsNum = false;
bool isPrevIsChar = false;
String result = "";
for (int i = 0; i < data.length; i++) {
bool isIntData = isInt(data[i]);
if (isIntData) {
isPrevIsChar = false;
if (isPrevIsNum) {
result = "$result${data[i]}";
} else {
result = "$result ${data[i]}";
isPrevIsNum = true;
}
} else {
isPrevIsNum = false;
if (isPrevIsChar) {
result = "$result${data[i]}";
} else {
result = "$result ${data[i]}";
}
isPrevIsChar = true;
}
}
print(result.trim()); // KL 11 AB 2432
}
Condition can be merged on upper level I think, but to make it clear I've kept like this. There might be others short way 😅.
3 Comments
rasityilmaz
Heey I was already checking this with tryParse. Are you stealing my answer? :)
Md. Yeasin Sheikh
I've no intention on stealing someone answer, I was engaged multiple question, this might be delay, also I saved the question because I was curious about it.
rasityilmaz
I'm not serious man. I was joking
this is an minimal code efficient O(n) solution that I tested it :
expect(trimNumString("KL11AB2432"), "KL 11 AB 2432"); // true
and here it is :
String trimNumString(String text, {String separator = " "}) {
String result = "";
List<String> textAsList = text.split("");
for (int index = 0; index < testAsList.length; index += 1) {
bool hasReachedTheEdOfList = (index + 1) >= testAsList.length;
bool hasNotReachedTheEdOfList = !hasReachedTheEdOfList;
int? currentElement = int.tryParse(testAsList[index]);
int? nextElement;
if (hasNotReachedTheEdOfList) {
nextElement = int.tryParse(testAsList[index + 1]);
} else {
separator = "";
}
if ((currentElement == null && nextElement is int) ||
(currentElement is int && nextElement == null)) {
result += testAsList[index] + separator;
continue;
}
result += testAsList[index];
}
return result;
}
you can change the separator you want from the method call like this
enter code here
trimNumString("KL11AB2432", separator: " ");
ascii-codeto detect the number