创建字符串
要创建一个字符串,我们可以使用单引号或双引号或三引号:
void main() { String a = \'abc\'; //单引号 String b = \"abc\"; //双引号 //三引号 String c = \'\'\' abc def \'\'\';}
字符串拼接
多个字符串可使用 + 号进行拼接
void main() { String a = \'abc\'; String b = \"abc\"; print(a + b);}
获取单个字符
可使用下标的方式获取单个字符
void main() { String a = \'abc\'; print(a[1]);}
常用的属性
属性 | 类型 | 描述 | codeUnits | List<int> | 获取字符串utf-16编码值的列表 | hashCode | int | 获取字符派生的哈希代码 | isEmpty | bool | 字符串是否为空 | isNotEmpty | bool | 字符串是否不为空 | length | int | 获取字符串长度 | runes | Runes | 获取字符串utf-16编码值的可迭代列表 | runtimeType | Type | 获取运行时的数据类型 |
void main() { String a = \'abcdefg\'; print(a.codeUnits); print(a.hashCode); print(a.isEmpty); print(a.isNotEmpty); print(a.length); print(a.runes); print(a.runtimeType); //执行结果 //[97, 98, 99, 100, 101, 102, 103] //180814200 //false //true //7 //(97, 98, 99, 100, 101, 102, 103) //String}
常用的属性
方法 | 类型 | 描述 | codeUnitAt(int index) | int | 返回给定索引值的对应utf-16编码 | compareTo(String other) | int | 与传入字符串进行比较,有相同返回1,否则返回-1 | contains(Pattern other, [int index]) | bool | 查找返回字符串是否有符号要求的,传入index规定从index位开始查找 | endsWith(String other) | bool | 字符串的结尾是否为传入的值 | indexOf(Pattern other, [int start]) | int | 从字符串前面开始查找返回符合规则的第一个的索引位置,传入start规定从哪里开始查找 | lastIndexOf(Pattern other, [int start]) | int | 与indexOf相同,不同的是这个方法是从后面开始查找 | padLeft(int width, [String padding]) | String | 如果字符串没有width的长度,则在前面加上padding字符串并返回,不会改变原字符串 | padRight(int width, [String padding]) | String | 同padLeft方法相同,不同的是从padding加在后面 | replaceAll(Pattern from, String to) | String | 替换所有匹配的子字符串 | replaceAllMapped(Pattern from, String Function(Match match) replace) | String | 将匹配到的字符串用函数处理后返回字符串替换 | replaceFirst(Pattern from, String to, [int index]) | String | 替换第一个匹配的子字符串,可以规定从index出开始匹配 | replaceFirstMapped(Pattern from, String replace(match), [int index]) | String | 同replaceAllMapped,此方法只替换第一个匹配的值,可以规定从index处开始匹配 | replaceRange(int start, int end, String to) | String | 替换范围内的字符串 | split(Pattern pattern) | List<String> | 拆分字符串为数组 | splitMapJoin(Pattern pattern, { String onMatch(match), String onNonMatch(match) }) | String | 拆分替换字符串,匹配和不匹配的执行对应函数替换成返回值 | startsWith(Pattern pattern, [int index]) | bool | 是否是匹配的正则或字符串开始,可以规定从index开始查找 | substring(int startIndex, [int endIndex]) | String | 提取字符串中startIndex(包含)到endIndex(不包含)两个指定的索引号之间的字符。 | toLowerCase() | String | 把字符串转换为小写 | toUpperCase() | String | 把字符串转换为大写 | trim() | String | 去除字符串两边的空白 | trimLeft() | String | 去除字符串左边的空白 | trimRight() | String | 去除字符串右边的空白 |
void main() { String a = \' abacdfefg \'; print(a.codeUnitAt(0)); print(a.compareTo(\'other\')); print(a.contains(\'b\')); print(a.endsWith(\'f\')); print(a.indexOf(\'a\')); print(a.lastIndexOf(\'f\')); print(a.padLeft(20, \'0\')); print(a.padRight(20, \'0\')); print(a.replaceAll(r\'a\', \'h\')); print(a.replaceAllMapped(r\'a\', (d) => d.hashCode.toString())); print(a.replaceFirst(r\'a\', \'h\')); print(a.replaceFirstMapped(r\'a\', (d) => d.hashCode.toString())); print(a.replaceRange(0, 5, \'h\')); print(a.split(r\'f\')); print(a.splitMapJoin(r\'f\')); print(a.startsWith(r\'f\', 2)); print(a.substring(2, 6)); print(a.toLowerCase()); print(a.toUpperCase()); print(a.trim()); print(a.trimLeft()); print(a.trimRight());//输出结果//32//-1//true//false//2//9//0000000 abacdfefg// abacdfefg 0000000// hbhcdfefg// 336578979b372031767cdfefg// hbacdfefg// 994399439bacdfefg//hcdfefg//[ abacd, e, g ]// abacdfefg//false//abac// abacdfefg// ABACDFEFG//abacdfefg//abacdfefg// abacdfefg}