isTwoChineseCharacters property

bool get isTwoChineseCharacters

判断字符串是否为 2 个连续的中文字符

Implementation

bool get isTwoChineseCharacters {
  // 获取所有 Unicode 码点(自动处理代理对)
  final codePoints = runes.toList();

  // 条件1:必须恰好包含2个码点(即2个独立汉字)
  if (codePoints.length != 2) return false;

  // 条件2:每个码点必须在汉字的 Unicode 范围内
  for (final codePoint in codePoints) {
    // 基本区(U+4E00-U+9FFF) + 扩展区(U+3400-U+4DBF, U+20000-U+2EBFF 等)
    if (!((codePoint >= 0x4E00 && codePoint <= 0x9FFF) ||
        (codePoint >= 0x3400 && codePoint <= 0x4DBF) ||
        (codePoint >= 0x20000 && codePoint <= 0x2EBFF))) {
      return false;
    }
  }

  return true;
}