Trim只是空白,但不是飞镖中的换行符

问题描述 投票:0回答:1

我只想从字符串中修剪空格(0x20),但是Dart的trim文档说它将删除一组字符:

Here is the list of trimmed characters according to Unicode version 6.3:
   * ```
   *     0009..000D    ; White_Space # Cc   <control-0009>..<control-000D>
   *     0020          ; White_Space # Zs   SPACE
   *     0085          ; White_Space # Cc   <control-0085>
   *     00A0          ; White_Space # Zs   NO-BREAK SPACE
   *     1680          ; White_Space # Zs   OGHAM SPACE MARK
   *     2000..200A    ; White_Space # Zs   EN QUAD..HAIR SPACE
   *     2028          ; White_Space # Zl   LINE SEPARATOR
   *     2029          ; White_Space # Zp   PARAGRAPH SEPARATOR
   *     202F          ; White_Space # Zs   NARROW NO-BREAK SPACE
   *     205F          ; White_Space # Zs   MEDIUM MATHEMATICAL SPACE
   *     3000          ; White_Space # Zs   IDEOGRAPHIC SPACE
   *
   *     FEFF          ; BOM                ZERO WIDTH NO_BREAK SPACE
string flutter dart trim removing-whitespace
1个回答
0
投票

不是快速解决方案。

import 'package:enumerable/enumerable.dart';

void main() {
  final s0 = '  \nabc\ndef ';
  final codeUnits = s0.codeUnits;
  bool isBlank(int c) {
    return c == 32;
  }

  final q = codeUnits
      .skipWhile(isBlank)
      .skipLast(codeUnits.reverse().takeWhile(isBlank).count());
  final s1 = String.fromCharCodes(q.toList());
  print('"$s1"');
}

结果:

”abcdef”

甚至如此(仅修剪空白(0x20):]

import 'package:enumerable/enumerable.dart';

void main() {
  final s0 = '  \nabc\ndef ';
  final codeUnits = s0.codeUnits;
  final q = codeUnits
      .skipWhile((e) => e == 32)
      .skipLast(codeUnits.reverse().takeWhile((e) => e == 32).count());
  final s1 = String.fromCharCodes(q.toList());
  print('"$s1"');
}
© www.soinside.com 2019 - 2024. All rights reserved.