本节目标

  • 声明方式
  • 字符串模板
  • 字符串连接
  • 转义操作
  • 其它常用运算

环境

  • Dart 2.0.0

单引号或者双引号

1
2
String a = 'ducafecat';
String b = "ducafecat";

字符串模板

1
2
3
var a = 123;
String b = 'ducafecat : ${a}';
print(b);

字符串连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var a = 'hello' + ' ' + 'ducafecat';
var a = 'hello'' ''ducafecat';
var a = 'hello' ' ' 'ducafecat';
var a = 'hello'
' '
'ducafecat';
var a = '''
hello word
this is multi line
''';
var a = """
hello word
this is multi line
""";
print(a);

转义符号

1
2
var a = 'hello word \n this is multi line';
print(a);

取消转义

1
2
var a = r'hello word \n this is multi line';
print(a);

搜索

1
2
3
4
5
var a = 'web site ducafecat.tech';
print(a.contains('ducafecat'));
print(a.startsWith('web'));
print(a.endsWith('tech'));
print(a.indexOf('site'));

提取数据

1
2
3
4
print(a.substring(0,5));
var b = a.split(' ');
print(b.length);
print(b[0]);

大小写转换

1
2
print(a.toLowerCase());
print(a.toUpperCase());

裁剪 判断空字符串

1
2
print('    hello word     '.trim());
print(''.isEmpty);

替换部分字符

1
print('hello word word!'.replaceAll('word', 'ducafecat'));

字符串创建

1
2
3
4
5
6
var sb = StringBuffer();
sb..write('hello word!')
..write('my')
..write(' ')
..writeAll(['web', 'site', 'https://ducafecat.tech']);
print(sb.toString());

代码

参考


© 猫哥

https://ducafecat.tech