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
作用: 判断字符串是否已XX开头
API源码:
bool startsWith(Pattern pattern, [int index = 0]);
参数一: Pattern
是String,RegExp 都去实现的抽象类。
参数二: 右偏移个数
例子:
var start = ‘我爱你中国’;
print(start.startsWith(‘你’, 2)); //true
也可以跟正则:
print(‘dart’.startsWith(RegExp(r’art’), 1)); //true
indexOf
作用: 匹配查找字符串的位置,返回int 位置
API源码:
/// Returns the position of the first match of [pattern] in this string,
/// starting at [start], inclusive:
/// var string = ‘Dartisans’;
/// string.indexOf(‘art’); // 1
/// string.indexOf(RegExp(r’[A-Z][a-z]')); // 0
/// Returns -1 if no match is found:
/// string.indexOf(RegExp(r’dart’)); // -1
/// The [start] must be non-negative and not greater than [length].
int indexOf(Pattern pattern, [int start = 0]);
搜索字符在字符串的位置, 参数类似 startsWith。
例子:
print(‘dart’.indexOf(‘art’)); //1
print(‘dart’.indexOf(‘t’)); //3
print(‘dart’.indexOf(‘b’)); //-1
print(‘dart’.indexOf(RegExp(r’dart’))); //0
print(‘dart’.indexOf(‘d’)); //0
print(‘dart’.indexOf(‘d’, 1)); //-1
print(‘aabbbcc’.indexOf(‘b’)); //2
print(‘aabbbcc’.indexOf(‘b’, 3)); //3
lastIndexOf
作用: 匹配查找字符串的最后一个的位置,返回int 位置
API源码:
/// The starting position of the last match [pattern] in this string.
///
/// Finds a match of pattern by searching backward starting at [start]:
/// var string = ‘Dartisans’;
/// string.lastIndexOf(‘a’); // 6
/// string.lastIndexOf(RegExp(r’a(r|n)')); // 6
/// Returns -1 if [pattern] could not be found in this string.
/// string.lastIndexOf(RegExp(r’DART’)); // -1
/// If [start] is omitted, search starts from the end of the string.
/// If supplied, [start] must be non-negative and not greater than [length].
int lastIndexOf(Pattern pattern, [int? start]);
同理 indexOf 只是会选择最后一个匹配的位置
isEmpty 和 isNotEmpty
作用: 判断字符串是否是空
API源码:
/// Whether this string is empty.
bool get isEmpty;
/// Whether this string is not empty.
bool get isNotEmpty;
因为空安全所以不存在null去判断这种情况。
例子:
String a = ‘’;
print(a.isEmpty);
print(a.isNotEmpty);
substring
作用: 字符串截取子字符串
API源码:
/// The substring of this string from [start],inclusive, to [end], exclusive.
///
/// Example:
/// var string = ‘dartlang’;
/// string.substring(1); // ‘artlang’
/// string.substring(1, 4); // ‘art’
String substring(int start, [int? end]);
例子:
var str = ‘我爱你中国’;
var subStr1 = str.substring(1);
print(subStr1);
var subStr2 = str.substring(1, 3);
print(subStr2);
打印结果:
爱你中国
爱你
trim
作用: 去除字符串空白格
‘\tDart is fun\n’.trim(); // ‘Dart is fun’
例子:
去除空白格返回的字符串还是之前的地址:
var str1 = ‘Dart’;
var str2 = str1.trim();
identical(str1, str2); // true
去除空白格的uniCode 值如下:
/// 0009…000D ; White_Space # Cc …
/// 0020 ; White_Space # Zs SPACE
/// 0085 ; White_Space # Cc
/// 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
trimLeft
和 trimRight
分别去除左边和右边。
乘号
作用: 就是字符串重复repeat
字符串 = 字符串 * 个数
API源码:
String operator *(int times);
例子:
var times = ‘哈’;
var timesRepeat = times * 10;
print(timesRepeat); //哈哈哈哈哈哈哈哈哈哈
padLeft 和 padRight
作用: 字符串补齐
API源码:
String padLeft(int width, [String padding = ’ ']);
String padRight(int width, [String padding = ’ ']);
例子:
多用于字符串补0处理,举例padleft,padRight同理。
var list = [];
for (var i = 0; i < 100; i++) {
list.add(i.toString().padLeft(4, ‘0’));
}
print(list); //[0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007, 0008, 0009, 0010, 0011, 0012, 0013, 0014, 0015,…]
contains
作用: 判断字符串是否包含子字符串或者正则
APi源码:
/// Example:
/// var string = ‘Dart strings’;
/// string.contains(‘D’); // true
/// string.contains(RegExp(r’[A-Z]')); // true
/// If [startIndex] is provided, this method matches only at or after that
/// index:
/// string.contains(‘D’, 1); // false
/// string.contains(RegExp(r’[A-Z]'), 1); // false
/// The [startIndex] must not be negative or greater than [length].
bool contains(Pattern other, [int startIndex = 0]);
例子:
print(‘Dart’.contains(‘D’)); // true
print(‘Dart’.contains(‘D’, 1)); // false
replaceFirst
作用: 替换掉第一个符合条件的字符
参数:
-
from 被替换字符支持正则查找和字符串 ;
-
to是要替换的字符;
-
startIndex开始位置
API源码:
String replaceFirst(Pattern from, String to, [int startIndex = 0]);
例子:
var replaceFristResult = ‘DartD’.replaceFirst(‘D’, ‘X’);
print(replaceFristResult);
var replaceFristResult1 = ‘DartD’.replaceFirst(‘D’, ‘X’, 1);
print(replaceFristResult1);
var replaceFristResult2 = ‘DartD’.replaceFirst(r’D’, ‘X’, 1);
print(replaceFristResult2);
replaceFirstMapped
作用: 相比replaceFirst,替换字符可以根据匹配match结果去替换,更加灵活。
API源码:
/// Replace the first occurrence of [from] in this string.
///
/// Returns a new string, which is this string
/// except that the first match of [from], starting from [startIndex],
/// is replaced by the result of calling [replace] with the match object.
///
/// The [startIndex] must be non-negative and no greater than [length].
String replaceFirstMapped(Pattern from, String replace(Match match),
[int startIndex = 0]);
例子:
String replace(Match match) {
print(match.start);
print(match.end);
print(match.groupCount);
print(match.input);
print(match.pattern);
return ‘X’;
}
var replaceFristResult = ‘DartD’.replaceFirstMapped(‘D’, replace, 1);
print(replaceFristResult);
replaceAll和 replaceAllMapped
作用: 类似replaceFirst ,replaceAllMapped只是全部进行替换。
API源码:
/// Replaces all substrings that match [from] with [replace].
///
/// Creates a new string in which the non-overlapping substrings matching
/// [from] (the ones iterated by from.allMatches(thisString)
) are replaced
/// by the literal string [replace].
/// ‘resume’.replaceAll(RegExp(r’e’), ‘é’); // ‘résumé’
/// Notice that the [replace] string is not interpreted. If the replacement
/// depends on the match (for example on a [RegExp]'s capture groups), use
/// the [replaceAllMapped] method instead.
String replaceAll(Pattern from, String replace);
/// Replace all substrings that match [from] by a computed string.
///
/// Creates a new string in which the non-overlapping substrings that match
/// [from] (the ones iterated by from.allMatches(thisString)
) are replaced
/// by the result of calling [replace] on the corresponding [Match] object.
///
/// This can be used to replace matches with new content that depends on the
/// match, unlike [replaceAll] where the replacement string is always the same.
///
/// The [replace] function is called with the [Match] generated
/// by the pattern, and its result is used as replacement.
///
/// The function defined below converts each word in a string to simplified
/// ‘pig latin’ using [replaceAllMapped]:
/// pigLatin(String words) => words.replaceAllMapped(
/// RegExp(r’\b(\w*?)([aeiou]\w*)', caseSensitive: false),
/// (Match m) => “ m [ 2 ] {m[2]} m[2]{m[1]}${m[1]!.isEmpty ? ‘way’ : ‘ay’}”);
///
/// pigLatin(‘I have a secret now!’); // ‘Iway avehay away ecretsay ownay!’
String replaceAllMapped(Pattern from, String Function(Match match) replace);
replaceRange
作用: 替换固定范围的内容
API源码:
/// Replaces the substring from [start] to [end] with [replacement].
///
/// Creates a new string equivalent to:
/// this.substring(0, start) + replacement + this.substring(end)
/// The [start] and [end] indices must specify a valid range of this string.
/// That is 0 <= start <= end <= this.length
.
/// If [end] is null
, it defaults to [length].
String replaceRange(int start, int? end, String replacement);
例子:
var str = ‘hello world’;
print(str.replaceRange(6, 11, ‘dart’)); //hello dart
总结
本文从基础到高级再到实战,由浅入深,把MySQL讲的清清楚楚,明明白白,这应该是我目前为止看到过最好的有关MySQL的学习笔记了,我相信如果你把这份笔记认真看完后,无论是工作中碰到的问题还是被面试官问到的问题都能迎刃而解!
MySQL50道高频面试题整理:
了解详情https://docs.qq.com/doc/DSmxTbFJ1cmN1R2dB
([aeiou]\w*)', caseSensitive: false),
/// (Match m) => “ m [ 2 ] {m[2]} m[2]{m[1]}${m[1]!.isEmpty ? ‘way’ : ‘ay’}”);
///
/// pigLatin(‘I have a secret now!’); // ‘Iway avehay away ecretsay ownay!’
String replaceAllMapped(Pattern from, String Function(Match match) replace);
replaceRange
作用: 替换固定范围的内容
API源码:
/// Replaces the substring from [start] to [end] with [replacement].
///
/// Creates a new string equivalent to:
/// this.substring(0, start) + replacement + this.substring(end)
/// The [start] and [end] indices must specify a valid range of this string.
/// That is 0 <= start <= end <= this.length
.
/// If [end] is null
, it defaults to [length].
String replaceRange(int start, int? end, String replacement);
例子:
var str = ‘hello world’;
print(str.replaceRange(6, 11, ‘dart’)); //hello dart
总结
本文从基础到高级再到实战,由浅入深,把MySQL讲的清清楚楚,明明白白,这应该是我目前为止看到过最好的有关MySQL的学习笔记了,我相信如果你把这份笔记认真看完后,无论是工作中碰到的问题还是被面试官问到的问题都能迎刃而解!
MySQL50道高频面试题整理:
[外链图片转存中…(img-i43X7jVN-1724199237239)]
了解详情https://docs.qq.com/doc/DSmxTbFJ1cmN1R2dB