dart字符串属性方法
Dart 中的 String 编码格式是 UTF-16,也就是 16 位的 Unicode 转换格式的代码单元序列。
首先声明一个一个字符串
String str = 'Hello Axe';
一、字符串属性
一、常用属性
1、字符串长度
print(str.length); // 9
2、字符串是否为空
print(str.isEmpty); // false
3、字符串是否不为空
print(str.isNotEmpty); // true
4、运行时的数据类型
print(str.runtimeType); // String
二、非常用属性
1、当前字符串的 UTF-16 编码值的列表
print(str.codeUnits); // [72, 101, 108, 108, 111, 32, 65, 120, 101]
2、字符串派生的哈希代码
print(str.hashCode); // 154086258
3、字符串的 UTF-16 编码值的可迭代列表
print(str.runes); // (72, 101, 108, 108, 111, 32, 65, 120, 101)
二、字符串方法
1、字符串类型转化 toString
在 Dart 中继承于 Object 的类型,都支持一个方法那就是 toString()
,该方法可以将目标类型转换为 String 类型。
int a = 10;
print(a.toString()); // int 类型的 10 转换为 String 类型的 "10"
2、字符串查找
indexOf
int indexOf(Pattern pattern, [int start = 0]);
由前向后查找指定字符,返回值是 int 类型,如果有符合条件的则返回对应字符所在的索引,没有找到则返回 -1。
第一个参数是要查找的字符,支持正则表达式,后面如果看到 Pattern 类型的,都可以用正则表达式代替字符。
第二个为可选参数,从哪个索引处开始查找(包括该索引),默认为 0 ,其中 start 不能为负数,且不大于被查找字符的串长度。
print(str.indexOf('l')); // 2
lastIndexOf
int lastIndexOf(Pattern pattern, [int? start]);
由后向前查找指定字符,返回值是 int 类型,如果有符合条件的则返回对应字符所在的索引,没有找到则返回 -1。需要事先说明的一点,倒序查找不是说索引也倒过来了。
第一个参数是要查找的字符,第二个为可选参数,从哪个索引处开始查找(包括该索引),没有默认值。
String str = 'Hello Axe';
print(str.lastIndexOf('l')); // 3
print(str.lastIndexOf('e')); // 8
3、字符串判断
contains
bool contains(Pattern other, [int startIndex = 0]);
判断字符串中是否包含指定字符,存在返回 true,不存在返回 false。
第一个参数是指定的字符,第二个为可选参数,代表从哪个索引位置开始判断,默认为 0 ,其中 startIndex 不能为负数,且不大于字符串的长度,否则会报错。
String str = 'Hello Axe';
print(str.contains('e')); // true
print(str.contains('l', 3)); // true
print(str.contains('1', 4)); // false
print(str.contains('1', 10)); // RangeError: Invalid value: Not in inclusive range 0..9: 10
startWith
bool startsWith(Pattern pattern, [int index = 0]);
判断字符串是否以指定字符开头,成立返回 true,不成立返回 false。
第一个参数是指定的字符,第二个为可选参数,代表从哪个索引位置开始判断,默认为 0 ,其中 index 不能为负数,且不大于字符串的长度。
String str = 'Hello Axe';
print(str.startsWith('H')); // true
print(str.startsWith('H', 1)); // false
print(str.startsWith('e', 1)); // true
endsWith
bool endsWith(String other);
判断字符串是否以指定字符结尾,成立返回 true,不成立返回 false,参数 other 为指定的字符。
String str = 'Hello Axe';
print(str.endsWith('e')); // true
print(str.endsWith('x')); // false
// 判断字符串是否是以指定字符串结尾
print(str.endsWith('Axe')); // true
4、字符串分割
substring
String substring(int start, [int? end]);
第一个参数 start 是开始分割字符串的索引,当只传了 start 参数时,表示从 start 索引处截取到字符串末尾。
第二个参数 end 是可选参数,表示结束分割字符串的索引,end 的值不能大于字符串的长度。
字符串截取的范围是 [start,end),也就是含头不含尾。
String str = 'Hello Axe';
print(str.substring(2)); // llo Axe
print(str.substring(2, 7)); // llo A
split
List<String> split(Pattern pattern);
以指定字符分割字符串,返回类型为 List<String>
,被分割的字符不会添加到数组中。
如果指定的分割字符不在字符串中,则将整个字符串直接添加到数组中,作为新数组中的一个元素。
String str = 'Hello Axe';
print(str.split(' ')); // ['Hello', 'Axe']
print(str.split('')); // ['H', 'e', 'l', 'l', 'o', ' ', 'A', 'x', 'e']
print(str.split('+')); // ['Hello Axe']
splitMapJoin
String splitMapJoin(
Pattern pattern, {
String Function(Match)? onMatch,
String Function(String)? onNonMatch,
});
这个方法比较特殊,可以在一条语句中完成 3 件事,虽然平常用到的不多,但还是要知道这个方法的用途。
第一个参数可以是一个字符串 String 或一个正则 RegExp 对象。
第二个参数是可选参数,表示将每个和指定的分割字符匹配的,替换为自定义返回的字符串。
第三个参数也是可选参数,但和第二个参数刚好相反,表示将每个和指定的分割字符不匹配的,替换为自定义返回的字符串。
String str = 'Hello Axe';
// 将字符串中是空的替换为字符 ,
String val = str.splitMapJoin('',onMatch: (Match match) => ',');
print(val); // ,H,e,l,l,o, ,A,x,e,
// 将字符串中不是空的替换为字符H
String val2 = String val = str.splitMapJoin('',
onMatch: (Match match) => ',', onNonMatch: (String nonMatch) => 'H');
print(val2); // H,H,H,H,H,H,H,H,H,H,H
5、字符串大小写转化
toUpperCase
String toUpperCase();
将字符串中所有的小写字母转换为大写字母,已是大写字母或者不是小写字母的如数字,返回原字符。
String str = 'Hello Axe';
print(str.toUpperCase()); // HELLO AXE
toLowerCase
String toLowerCase();
将字符串中所有的大写字母转换为小写字母,已是小写字母或者不是大写字母的如数字,返回原字符。
String str = 'Hello Axe';
print(str.toLowerCase()); // hello axe
6、字符串补齐
padLeft
String padLeft(int width, [String padding = ' ']);
字符串左补齐,指定 width 后,如果有剩余位则使用指定字符替换。如果指定 width 小于字符串长度,则返回原字符串。
第一个参数 width 不是补齐后的字符串总长度,你可以用 width 减去原来字符串的长度,得到的结果表示将会在字符串左边添加多少个补齐的字符。
第二个参数 padding 为可选参数,表示要补齐的字符,不指定时默认补上空格。
String str = 'Hello Axe';
// 在指定字符串的左边,添加(10 - str.length = 1)个空格
print(str.padLeft(10)); // 返回 " Hello Axe",注意,字符 Hello Axe左边有1个空格
// 在指定字符串的左边,添加(10 - str.length = 1)个字符+
print(str.padLeft(10, '+')); // +Hello Axe
// 当表示时间的数字为各位数时,可通过左边补0达到显示双位数的效果
String min = '7';
print(min.padLeft(2, '0')); // 07
注意:第一个参数不是添加的个数,更不是添加的位置,而是用该参数减去当前字符串的长度后,得到的结果为要补齐的个数,如果width 小于字符串的长度,不做任何修改,返回原字符串。
padRight
String padRight(int width, [String padding = ' ']);
字符串右补齐,指定 width 后,如果有剩余位则使用指定字符替换。如果指定 width 小于字符串长度,则返回原字符串。
第一个参数 width 不是补齐后的字符串总长度,你可以用 width 减去原来字符串的长度,得到的结果表示将会在字符串右边添加多少个补齐的字符(见下面的 01、02、03 例子)
第二个参数 padding 为可选参数,表示要补齐的字符,不指定时默认补上空格。
String str = 'Hello Axe';
print(str.padRight(10)); // 'Hello Axe '
print(str.padRight(10, '+')); // Hello Axe+
7、字符串去除空格
trim
String trim();
去除字符串中左右两边的空格,注意不是去掉字符串中所有的空格。如果两边没有空格,则返回原字符串。
String str = " Hello Axe ";
print(str.trim()); // 'Hello Axe'
trimLeft
String trimLeft();
去除字符串中左边的空格,如果左边没有空格,则返回原字符串。
String str = " Hello Axe ";
print(str.trimLeft()); // 'Hello Axe '
trimRight
String trimRight();
去除字符串中右边的空格,如果右边没有空格,则返回原字符串。
String str = " Hello Axe ";
print(str.trimRight()); // ' Hello Axe'
8、字符串替换
replaceAll
String replaceAll(Pattern from, String replace);
替换掉字符串中全部符合条件的字符。如果没有符合条件的,则返回原字符串。
第一个参数 from 表示字符串中被替换掉的字符,第二个参数 replace 表示替换为什么字符。
String str = "Hello Axe";
// 替换字符串'xe'为'ex'
print(str.replaceAll('xe', 'ex')); // Hello Aex
// 将字符串中所有e替换为E
print(str.replaceAll('e', 'E')); // HEllo AxE
replaceFirst
String replaceFirst(Pattern from, String to, [int startIndex = 0]);
只替换掉第一个符合条件的字符。
第一个参数 from 表示字符串中被替换掉的字符,第二个参数 to 表示替换为什么字符,第三个参数为可选参数,表示从哪个索引位置开始替换,默认为 0 。
String str = "Hello Axe";
print(str.replaceFirst('e', 'E')); // HEllo Axe
print(str.replaceFirst('e', 'E', 2)); // Hello AxE
replaceRange
String replaceRange(int start, int? end, String replacement);
替换掉指定范围内的字符。有效取值范围是 0 <= start <= end <= 字符串.length
。
第一个参数 start 表示从哪个索引位置开始替换,包括该索引。
第二个参数 end 表示从哪个索引位置结束替换,不包括该索引。因为 end 为可空参数,当 end 为 null 时,表示到字符串末尾。
第三个参数 replacement 表示替换为什么字符。
String str = "Hello Axe";
print(str.replaceRange(0, 3, 'o')); // olo Axe
print(str.replaceRange(6, null, 'o')); // Hello o
replaceAllMapped
String replaceAllMapped(Pattern from, String Function(Match match) replace);
替换掉所有符合条件的字符。用方法的返回值替换指定的字符,如果没有匹配的字符,则返回原字符串。
第一个参数 from 表示字符串中被替换掉的字符,
第二个参数 replace 表示如果有匹配的字符,则用指定的字符作为返回值替,换掉字符串中符合条件的字符。最终的效果其实和方法 replaceAll 是一样的。
String str = "Hello Axe";
print(str.replaceAllMapped('Axe', (match) => 'World')); // Hello World
print(str.replaceAllMapped('e', (match) => 'E')); // HEllo AxE
replaceFirstMapped
String replaceFirstMapped(Pattern from, String replace(Match match), [int startIndex = 0]);
只替换掉第一个符合条件的字符。用方法的返回值替换指定的字符,如果没有匹配的字符,则返回原字符串。
第一个参数 from 表示字符串中被替换掉的字符,
第二个参数 replace 表示如果有匹配的字符,则用指定的字符作为返回值,替换掉字符串中第一个符合条件的字符。最终的效果其实和方法 replaceFirst 是一样的。
第三个参数 startIndex 为可选参数,表示从哪个索引位置开始替换,默认为 0,不能大于字符串的长度。
String str = "Hello Axe";
print(str.replaceFirstMapped('e', (match) => 'E')); // HEllo Axe
print(str.replaceFirstMapped('e', (match) => 'E', 2)); // Hello AxE
9、字符串比较
compareTo
int compareTo(String other);
比较字符在 ASCII 码的位置。
如果当前字符在被比较字符后面,返回 1,位置相等返回 0,如果当前字符在被比较字符前面,返回 -1。
在 ASCII 码中,48~57 为 0到9十个阿拉伯数字,65~90 为 26 个大写英文字母,97~122 为 26 个小写英文字母。
String text = "b";
print(text.compareTo('a')); // 返回1:因为b在a后面
print(text.compareTo('b')); // 返回0
print(text.compareTo('c')); // 返回-1
print(text.compareTo('8')); // 返回1:字母在数字的后面
10、获取字符串的unicode编码
codeUnitAt
int codeUnitAt(int index);
返回指定索引处字符的 Unicode 十进制编码。
String str = 'Hello Axe';
print(str.codeUnitAt(0)); // 72 索引 0 处的字符是 H,H 的 Unicode 十进制编码是 72
print(str.codeUnitAt(5)); // 32 索引 5 处的字符是空格,空格的 Unicode 十进制编码是 32
11、正则表达式 matchAsPrefix()
Match matchAsPrefix(String string, [int start])
将此模式与 string
的开头匹配。
如果模式匹配从 start
开始的 string
的子字符串,则返回匹配项,如果模式在该点不匹配,则返回 null
。
start
必须为非负数且不大于 string.length
。
String str = 'Hello Axe';
RegExp re = RegExp(r'He');
Match? strMatch = re.matchAsPrefix(str);
print(strMatch); // Match found.