abstract class String implements Comparable, Pattern
| 接口名称 | 用途 |
| — | — |
| Comparable | 对比接口 |
| Pattern | 搜索匹配接口 |
String 基于Comparable、Pattern具备其功能
fromCharCodes
charCodes 16位以内:
/// Allocates a new string containing the specified [charCode].
///
/// If the [charCode] can be represented by a single UTF-16 code unit, the new
/// string contains a single code unit. Otherwise, the [length] is 2 and
/// the code units form a surrogate pair. See documentation for
/// [fromCharCodes].
///
/// Creating a [String] with one half of a surrogate pair is allowed.
external factory String.fromCharCode(int charCode);
使用:
main() {
print(String.fromCharCode(97)); // a
}
charCodes 超过16位
源码函数:
/// If [start] and [end] are provided, only the values of [charCodes]
/// at positions from start
to, but not including, end
, are used.
/// The start
and end
values must satisfy
/// 0 <= start <= end <= charCodes.length
.
external factory String.fromCharCodes(Iterable charCodes,
[int start = 0, int? end]);
var clef = String.fromCharCodes([0x1D11E]);
clef.codeUnitAt(0); // 0xD834
clef.codeUnitAt(1); // 0xDD1E
fromEnvironment
作用: flutter 启动的时候可以设置环境变量,可以通过fromEnvironment获取环境变量的内容。
API源码:
external const factory String.fromEnvironment(String name,
{String defaultValue = “”});
例子:
const String.fromEnvironment(“defaultFloo”, defaultValue: “no floo”)
可以通过bool.hasEnvironment
判断是否存在
const maybeDeclared = bool.hasEnvironment(“maybeDeclared”)
-
? String.fromEnvironment(“maybeDeclared”)
- null;
length
API:
int get length;
例子:
‘Dart’.length; // 4
‘Dart’.runes.length; // 4
var clef = ‘\u{1D11E}’;
clef.length; // 2
clef.runes.length; // 1
== 等号操作符
检查每个code units 是否相等,不比较Unicode的等价性。
API源码:
bool operator ==(Object other);
compareTo
作用: 比较字符串是否相,返回整型,0相等,-1不相等。
API源码:
int compareTo(String other);
例子:
var a = ‘a’;
var b = ‘a’;
var c = ‘c’;
print(a.compareTo(b)); //0
print(a.compareTo©); //-1
endsWith
作用: 判断是否以XX字符串结尾
API源码:
bool endsWith(String other);
例子:
‘Dart’.endsWith(‘t’); // true
startsWith
作用: 判断