0

I want space between the numbers and letters in a string. example: String = "KL11AB2432"; to String = "KL 11 AB 2432";

1
  • you can loop and use ascii-code to detect the number Commented Oct 15, 2022 at 16:14

4 Answers 4

2

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(" ");
}
Sign up to request clarification or add additional context in comments.

Comments

0
  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

0

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

Heey I was already checking this with tryParse. Are you stealing my answer? :)
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.
I'm not serious man. I was joking
0

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: " ");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.