splitUntil static method

List<String> splitUntil(
  1. String text,
  2. String separator, {
  3. int maxParts = -1,
})

Implementation

static List<String> splitUntil(String text, String separator, {int maxParts = -1}) {
  List<String> result = [];
  if(maxParts == -1){
    return text.split(separator);
  }else{
    int startIndex = 0;
    int count = 0;
    while (count < maxParts) {
      int index = text.indexOf(separator, startIndex);
      if (index == -1) break;

      result.add(text.substring(startIndex, index));
      startIndex = index + separator.length;
      count++;
    }

    // 添加剩余部分
    result.add(text.substring(startIndex));
    return result;
  }
}