单引号或者双引号
String a = 'hello'
String b = "hello"
final myString = 'Bob's dog'; // Bob's dog
final myString = "a "quoted" word"
final myString = "Bob's dog"
final myString = 'a "quoted" word'
final value = '"quoted"'
final myString = "a $value word"
字符串模板
var a = 123
String b = 'hello : ${a}'
print(b)
字符串连接
var a = 'hello' + ' ' + 'hello'
var a = 'hello'' ''hello'
var a = 'hello' ' ' 'hello'
var a = 'hello'
' '
'hello'
var a = '''
hello word
this is multi line
'''
var a = """
hello word
this is multi line
"""
print(a)
转义符号
var a = 'hello word \n this is multi line'
print(a)
hello word
this is multi line
取消转义
var a = r'hello word \n this is multi line';
print(a);
hello word \n this is multi line
搜索
var a = 'web site hello.tech';
print(a.contains('hello'));
print(a.startsWith('web'));
print(a.endsWith('tech'));
print(a.indexOf('site'));
true
true
true
4
提取数据
var a = 'web site hello.tech'
print(a.substring(0,5))
var b = a.split(' ')
print(b.length)
print(b[0])
web s
3
web
大小写转换
var a = 'web site hello.tech'
print(a.toLowerCase())
print(a.toUpperCase())
web site hello.tech
WEB SITE HELLO.TECH
裁剪 判断空字符串
print(' hello word '.trim());
print(''.isEmpty);
hello word
true
替换部分字符
print('hello word word!'.replaceAll('word', 'hello'));
hello hello hello!
字符串创建
var sb = StringBuffer();
sb..write('hello word!')
..write('my')
..write(' ')
..writeAll(['web', 'site', 'https://baidu.com']);
print(sb.toString());
hello word!my websitehttps://baidu.com