removeBetweenSpace static method

String removeBetweenSpace(
  1. String str, {
  2. bool removeLine = true,
})

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

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

Implementation

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