创建字符串

要创建一个字符串,我们可以使用单引号或双引号或三引号:

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
}