splitByKeyList static method

List<String> splitByKeyList(
  1. List<String> keyList,
  2. String text
)

Implementation

static List<String> splitByKeyList(List<String> keyList, String text) {
  List<String> splitResultByKeyList = [];
  if (text == null || text.isEmpty) {
    return splitResultByKeyList;
  }

  if (keyList == null || keyList.isEmpty) {
    splitResultByKeyList.add(text);
    return splitResultByKeyList;
  }

  int fromIndex = 0;
  for (String key in keyList) {
    int keyIndex = text.indexOf(key, fromIndex);
    if (keyIndex >= 0) {
      if (fromIndex < keyIndex) {
        String resultBeforeKeyCharacter = text.substring(fromIndex, keyIndex);
        splitResultByKeyList.add(resultBeforeKeyCharacter);
      }
      splitResultByKeyList.add(key);
      fromIndex = keyIndex + key.length;
    }
  }

  if (fromIndex < text.length) {
    splitResultByKeyList.add(text.substring(fromIndex));
  }

  return splitResultByKeyList;
}