今日心情很低落 T.T,所以参考官方文档 ,略微整理了一下 Dart String、List、Map、Date的常用方法。
String substring 1 2 3 4 5 6 7 var string = 'Dart ' + 'is ' + 'fun!' ; string.substring(0 , 5 ); string.substring(5 ); string.substring(string.length - 4 ); print ('$string 的长度是:${string.length} ' );string[0 ];
codeUnitAt/codeUnits 1 2 3 string = 'Dart' ; string.codeUnitAt(0 ); string.codeUnits;
isEmpty/isNotEmpty 字符串是否为空,return bool。
contains 1 2 'doubleam' .contains('am' ); 'doubleam' .contains('o' , 2 );
indexOf/lastIndexOf 1 2 'doubleam' .indexOf('d' ); 'doubleam' .lastIndexOf('x' );
padLeft/padRight 1 2 'hxb' .padLeft(6 , 'x' ); 'hxb' .padRight(6 , 'x' );
startsWith/endsWith 1 2 3 'hxb love oqm' .startsWith('hxb' ); 'hxb love oqm' .startsWith('love' , 4 ); 'hxb love oqm' .endsWith('oqm' );
replaceAll 1 2 3 4 "Hello World" .replaceAll('World' , 'Dart' ); RegExp testReg = new RegExp (r"^\w{5}$" );"Hello World" .replaceAll(testReg, '*******' );
compareTo 1 2 3 4 5 "C" .compareTo('C' ); "C" .compareTo('CC' ); "C" .compareTo('BBB' ); "C" .compareTo('B' );
toLowerCase/toUpperCase 1 2 'aBc' .toLowerCase(); 'aBc' .toUpperCase();
trim trimLeft/trimRight 1 2 ' abc ' .trimLeft(); ' abc ' .trimRight();
split List isEmpty/isNotEmpty List 是否为空,return bool。
first/last 1 2 3 [1 , 2 , 3 , 4 ].first; [1 , 2 , 3 , 4 ].last; [1 , 2 , 3 ].reversed.toList();
add 1 2 3 List testList = [1 , 2 , 3 ];testList.add(4 ); print (testList);
addAll 1 2 3 List testList = [1 , 2 , 3 ];testList.addAll([1 , 2 , 3 ]); print (testList);
any 1 2 List testList = [1 , 2 , 3 ];testList.any((item) => item > 1 );
every 1 2 [1 , 2 , 1 ].every((item) => item == 1 ); [1 , 1 , 1 ].every((item) => item == 1 );
where 1 [1 , 2 , 3 ].where((item) => item > 1 ).toList();
asMap 1 2 List testList = [1 , 2 , 3 ];testList.asMap();
clear 1 2 3 List testList = [1 , 2 , 3 ];testList.clear(); print (testList);
contains firstWhere 1 [1 , '2020' , 1 ].firstWhere((item) => item == '2020' );
indexWhere/lastIndexWhere 1 2 [1 , 2 , 3 ].indexWhere((item) => item == 3 , 2 ); [1 , 2 , 1 ].lastIndexWhere((item) => item == 1 );
indexOf/lastIndexOf 1 2 [1 , 2 , 3 ].indexOf(9 ); [1 , 2 , 1 ].lastIndexOf(1 );
insert 1 2 3 List testList = [1 , 3 , 4 ];testList.insert(1 , 2 ); print (testList);
insertAll 1 2 3 List testList = [1 , 4 , 5 ];testList.insertAll(1 , [2 , 3 ]); print (testList);
elementAt 1 2 List testList = [1 , 2 , 3 ];testList.elementAt(0 ) == testList[0 ];
expand 1 2 3 4 5 6 7 List testList1 = [[1 , 2 ], [3 ], [4 , 5 ]];List flatList= testList1.expand((item) => item).toList();print (flatList); List testList2 = [1 , 2 , 3 ];List computed = testList2.expand((item) => [item * 10 ]).toList();print (computed);
fillRange 1 2 3 List testList = [1 , 2 , 3 ];testList.fillRange(0 , 3 , 10 ); print (testList);
from 1 2 3 List testList = [1 , 2 , 3 ];var clonedList = List .from(testList);print (clonedList);
forEach 1 [1 , 2 , 3 ].forEach((item) => print (item));
map 1 2 List testList = [1 , 2 , 3 ];print (testList.map((item) => item * 100 ).toList();
reduce/fold 1 2 3 [1 , 2 , 3 ].reduce((item, nextItem) => item + nextItem); [1 , 2 , 3 ].fold(10 , (item, nextItem) => item + nextItem);
toSet 1 2 [1 , 1 , 2 , 2 , 3 ].toSet().toList();
remove 1 2 3 4 List testList = [1 , 2 , 3 , 1 ];testList.remove(1 ); print (testList);
removeAt 1 2 3 4 List testList = [1 , 2 , 3 , 1 ];print (testList.removeAt(0 )); print (testList);
removeLast 1 2 3 4 List testList = [1 , 2 , 3 , 1 ];print (testList.removeLast()); print (testList);
removeWhere 1 2 3 List testList = [1 , 2 , 3 , 1 ];testList.removeWhere((item) => item == 1 ); print (testList);
removeRange 1 2 3 List testList = [1 , 2 , 3 , 1 ];testList.removeRange(0 , 3 ); print (testList);
getRange 1 2 List testList = [1 , 2 , 3 , 1 ];print (testList.getRange(0 , 3 ).toList());
join replaceRange 1 2 3 List testList = [1 , 2 , 3 , 4 , 5 , 6 ];testList.replaceRange(0 , 3 , [10 , 11 , 12 , 13 ]); print (testList);
shuffle 1 2 3 List testList = [1 , 2 , 3 , 4 , 5 , 6 ];testList.shuffle(); print (testList);
setAll 1 2 3 List testList = [1 , 2 , 3 , 4 , 5 , 6 ];testList.setAll(1 , [0 , 0 , 0 ]); print (testList);
setRange 1 2 3 List testList = [1 , 2 , 3 , 4 , 5 , 6 ];testList.setRange(0 , 3 , [0 , 0 , 0 ]); print (testList);
take/skip 1 2 3 4 5 List testList = [1 , 2 , 3 , 4 , 5 , 6 ];print (testList.take(3 ).toList()); print (testList.skip(4 ).toList()); print (testList.take(3 ).skip(2 ).take(1 ).toList());
sort 1 2 3 4 5 List testStringList = ['a' , 'aaa' , 'A' , 'BBB' , 'C' , 'z' ];List testStringList.sort((item, nextItem) => item.compareTo(nextItem));testList.sort((item, nextItem) => item.compareTo(nextItem)); print (testList); print (testStringList);
sublist 1 2 3 List testList = [1 , 2 , 3 , 4 , 5 , 6 ];print (testList.sublist(0 , 3 )); print (testList);
Map entries 1 2 3 Map testMap = {'a' :1 , 'b' :2 , 'c' :3 };print (testMap.entries.toList());
keys 1 2 3 Map testMap = {'a' :1 , 'b' :2 , 'c' :3 };print (testMap.keys.toList());
values 1 2 3 Map testMap = {'a' :1 , 'b' :2 , 'c' :3 };print (testMap.values.toList());
isEmpty/isNotEmpty Map 是否为空,return bool。
addAll 1 2 3 4 5 6 7 8 9 Map testMap = {'a' :1 , 'b' :2 , 'c' :3 };testMap.addAll({'a' :100 , 'd' :99 }); print (testMap); testMap = { ...testMap, 'a' : 'hello' , 'x' : 'world' }; print (testMap);
addEntries 1 2 3 Map testMap = {'a' :1 , 'b' :2 , 'c' :3 };testMap.addEntries({'a' :100 , 'd' :99 }.entries); print (testMap);
clear 1 2 3 Map testMap = {'a' :1 , 'b' :2 , 'c' :3 };testMap.clear(); print (testMap);
containsKey 1 2 3 Map testMap = {'a' :1 , 'b' :2 , 'c' :3 };testMap.containsKey('a' ); testMap.containsKey('d' );
containsValue 1 2 3 Map testMap = {'a' :1 , 'b' :2 , 'c' :3 };testMap.containsValue(1 ); testMap.containsValue('1' );
forEach 1 2 3 4 Map testMap = {'a' :1 , 'b' :2 , 'c' :3 };testMap.forEach((key, value) { print ('$key => $value ' ); });
map 1 2 3 4 5 Map testMap = {'a' :1 , 'b' :2 , 'c' :3 };Map newMap = testMap.map((key, value){ return MapEntry(value, key); }); print (newMap);
putIfAbsent 1 2 3 4 5 Map testMap = {'a' :1 , 'b' :2 , 'c' :3 };testMap.putIfAbsent('a' , () => 'hello' ); testMap.putIfAbsent('d' , () => 'world' ); print (testMap);
remove 1 2 3 4 Map testMap = {'a' :1 , 'b' :2 , 'c' :3 };testMap.remove('a' ); print (testMap);
removeWhere 1 2 3 4 Map testMap = {'a' :1 , 'b' :2 , 'c' :3 };testMap.removeWhere((key, value) => value > 1 ); print (testMap);
update 1 2 3 Map testMap = {'a' :1 , 'b' :2 , 'c' :3 };testMap.update('a' , (value) => value + 100 ); print (testMap);
updateAll 1 2 3 Map testMap = {'a' :1 , 'b' :2 , 'c' :3 };testMap.updateAll((key, value) => value * 100 ); print (testMap);
from 1 2 3 4 Map testMap = {'a' :1 , 'b' :2 , 'c' :3 };Map newMap = Map .from(testMap);print (testMap); print (newMap);
Date 基础 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 new DateTime .now(); DateTime (2020 , 8 , 12 , 12 , 12 , 30 ); DateTime .parse('2020-01-01 09:30:36' ); DateTime .tryParse('2020-1-01 09:30:36' ); DateTime time = DateTime .parse('2020-01-11 09:30:36' );print (time.hashCode); print (time.year); print (time.month); print (time.day); print (time.hour); print (time.minute); print (time.second); print (time.millisecond); print (time.microsecond); print (time.weekday); print (time.isUtc); print (time.millisecondsSinceEpoch); print (time.microsecondsSinceEpoch); String timeSince(DateTime date, {bool longago = false , formater = "yyyy-mm-dd hh:ii:ss" }) { DateTime now = new DateTime .now(); if (now.isBefore(date)) { return dateFormat(date, format: formater); } var interval = now.difference(date); if (longago) { int months = interval.inDays ~/ 30 ; if (months >= 4 ) { return dateFormat(date, format: formater); } if (months >= 1 ) { return "$months 月前" ; } int weeks = interval.inDays ~/ 7 ; if (weeks >= 1 ) { return "$weeks 周前" ; } } if (interval.inDays >= 8 ) { return dateFormat(date, format: formater); } if (interval.inDays >= 1 ) { return "${interval.inDays} 天前" ; } if (interval.inHours >= 1 ) { return "${interval.inHours} 小时前" ; } if (interval.inMinutes >= 1 ) { return "${interval.inMinutes} 分钟前" ; } return "刚刚" ; }
以上代码中,使用正则进行时间转换的方法 dateFormat ,可以通过链接前往参考哦…
add 1 2 3 4 DateTime .now().add(Duration (minutes: 10 ));DateTime .now().add(Duration (hours: -1 ));
subtract 1 2 3 4 DateTime .now().subtract(Duration (minutes: -10 ));DateTime .now().subtract(Duration (hours: 1 ));
compareTo 1 2 3 4 DateTime now = DateTime .now();now.compareTo(now.add(Duration (hours: -1 ))); now.compareTo(now); now.compareTo(now.add(Duration (minutes: 10 )));
difference 1 2 3 4 5 6 7 DateTime now = DateTime .now();now.difference(now.add(Duration (hours: -1 ))); var differenceObj1 = now.add(Duration (minutes: 10 )).difference(now); var differenceObj2 = now.difference(now.add(Duration (minutes: 10 ))); print ([differenceObj1.inDays, differenceObj1.inHours, differenceObj1.inMinutes]); print ([differenceObj2.inDays, differenceObj2.inHours, differenceObj2.inMinutes]);
isAfter 1 2 DateTime now = DateTime .now();now.isAfter(now.add(Duration (hours: -1 )));
isBefore 1 2 DateTime now = DateTime .now();now.isBefore(now.add(Duration (hours: -1 )));
toIso8601String 1 DateTime .now().toIso8601String();
toLocal/toString/toUtc 1 2 3 print (DateTime .now().toLocal()); print (DateTime .now().toString()); print (DateTime .now().toUtc());
is-print-log 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 var test = 123 ;if (test is String ) { print ('${test} is string' ); } else if (value is int ) { print ('${test} is int' ); } else if (value is double ) { print ('${test} is double' ); } else { print ('${test} is other type' ); } print ('hello world!' );log('hello world!' );