Dart语法基础系列五《String 源码API详解》

abstract class String implements Comparable, Pattern

| 接口名称 | 用途 |

| — | — |

| Comparable | 对比接口 |

| Pattern | 搜索匹配接口 |

String 基于Comparable、Pattern具备其功能

String源码接口



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

作用: 判断

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值