removeBetweenSpace static method

String removeBetweenSpace(
  1. String str, {
  2. bool removeLine = true,
  3. bool subLeft = true,
  4. bool subRight = true,
})

移除str两边的(空格|制表符\t)

  • removeLine 是否移除两边的换行符号

Implementation

static String removeBetweenSpace(
  String str, {
  bool removeLine = true,
  bool subLeft = true,
  bool subRight = true,
}) {
  assert(subLeft || subRight);
  if (str.isEmpty) {
    return str;
  }
  int left = 0, right = str.length - 1;
  if (subRight) {
    for (; right >= left; --right) {
      if (str[right] != ' ' &&
          str[right] != '\t' &&
          (false == removeLine ||
              (str[right] != '\r' && str[right] != '\n'))) {
        break;
      }
    }
  }
  if (subLeft) {
    for (; left <= right; ++left) {
      if (str[left] != ' ' &&
          str[left] != '\t' &&
          (false == removeLine || (str[left] != '\r' && str[left] != '\n'))) {
        break;
      }
    }
  }
  if (left <= right) {
    return str.substring(left, right + 1);
  } else {
    return "";
  }
}