Dart笔记(5):字符串String

定义

单引号或者双引号

String a = 'abc';
String b = "abc";

字符串模板

var a = 123;
String b = 'abc: ${a}';
print(b);//abc:123

字符串连接

下面几种方式都可以拼接

var a = 'hello' + ' ' + 'world';
var a = 'hello'' ''world';
var a = 'hello'   ' '     'world';
var a = 'hello'
' '
'world';
var a = '''
hello world
this is multi line
''';
var a = """
hello world
this is multi line
""";
print(a);

转义符号

var a = 'hello world \n this is multi line';
print(a);//换行

取消转义符号 

var a = r'hello world \n this is multi line';
print(a);//不换行,转义符号也会打印出来

搜索

var a = 'hello world';
print(a.contains('wo'));    //true
print(a.startsWith('h'));    //true    
print(a.endsWith('d'));    //true
print(a.indexOf('llo'));    //2

提取数据

print(a.substring(0,5));
var b = a.split(' ');

大小写转换

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

裁剪 判断空字符串

print('    hello world     '.trim());
print(''.isEmpty);

替换部分字符串

print('hello world world!'.replaceAll('world', 'abc'));

字符串创建

var sb = StringBuffer();
sb..write('hello world!')
..write('my')
..write(' ')
..writeAll(['aa', 'bb']);
print(sb.toString());//hello world!my aabb

 

 

你可能感兴趣的:(Dart学习,Dart基础学习)