常平做网站,做搜狗网站排名软,广东购物网站建设价格,烟台网站建设公司报价数值类型 int、double、num
int
整型#xff0c;取值通常在 -253 ~ 253 之间 int class
double
64-bt(双精度)浮点数#xff0c;符合 IEEE 754 标准。 double class
num
数值类型的基类#xff0c;int 和 double 都继承自num。 num class
数值转换
// String - …数值类型 int、double、num
int
整型取值通常在 -253 ~ 253 之间 int class
double
64-bt(双精度)浮点数符合 IEEE 754 标准。 double class
num
数值类型的基类int 和 double 都继承自num。 num class
数值转换
// String - int
int a int.parse(123);
// String - double
double b double.parse(1.234);// int - String
String a 123.toString();
// double - String
String b 1.234.toString();
print([a, b]);// double - int
double a 1.23;
int b a.toInt();
print(b);布尔 bool
为了代表布尔值Dart 有一个名字为 bool 的类型。只有两个对象是布尔类型true 和 false 所创还能得对象这两个对象也是编译时常量。
bool a;
print(a);true 判断
只有 true 对象才被认为是 true。所有其他的值都是 false。
String name 123;
if (name) {print(this is name); // 不执行
}assert 断言
var a true;
assert(a);var name ;
assert(name.isEmpty); // 是否为空
assert(name.isNotEmpty); // 是否不为空var num 0 / 0;
assert(num.isNaN);断言只有在 Debug 模式下运行有效如果在 Release 模式下运行则断言不会执行。
字符串 String
字符串的创建
final myString Bob\s dog; // Bobs dog
final myString a \quoted\ word; // a quoted wordfinal myString Bobs dog; // Bobs dog
final myString a quoted word; // a quoted wordfinal value quoted; // quoted
final myString a $value word; // a quoted word单引号与双引号都可创建 String 对象。在单引号内部如果使用单引号则需要转义而使用双引号则不需要转义。反之亦然。在String中可以使用 $ 或 ${} 引入某个对象进行字符串转义。
字符串的连接
var a hello word;
var a hello word;
var a hello word;var a helloword;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 wordthis is multi line在字符串内部使用 \ 即为转义
取消转义
var a rhello word \n this is multi line;
print(a);hello word \n this is multi line取消转义即在字符串前添加 r 修饰
字符串搜索
var a web site ducafecat.tech;
print(a.contains(ducafecat)); // 是否包含该字符串
print(a.startsWith(web)); // 是否以 web 开头
print(a.endsWith(tech)); // 是否以 tech 结尾
print(a.indexOf(site)); // site 在字符串中的位置true
true
true
4StringBuffer 使用
var sb StringBuffer();
sb..write(hello word!)
..write(my)
..write( )
..writeAll([web, site, https://ducafecat.tech]);
print(sb.toString());hello word!my websitehttps://ducafecat.techStringBuffer 是存储 String 的桶子必需使用 toString() 函数才能得到 String 类型
DateTime 时间
创建
当前时间
var now new DateTime.now();
print(now);2023-05-28 14:37:43.607指定年月日
var date new DateTime(2008, 10, 01, 9, 30);
print(date);2008-10-01 09:30:00.000协调世界时 UTC
var d new DateTime.utc(2008, 10, 01, 9, 30);
print(d);2008-10-01 09:30:00.000Z解析时间
var d DateTime.parse(2008-10-01 09:30:30Z);
print(d);2008-10-01 09:30:30.000Zvar d DateTime.parse(2008-10-01 09:30:300800);
print(d);2008-10-01 01:30:30.000Z时间增减
var d DateTime.now();
print(d);
print(d.add(new Duration(minutes: 5)));
print(d.add(new Duration(minutes: -5)));2023-10-01 22:09:12.805
2023-10-01 22:14:12.805
2023-10-01 22:04:12.805时间比较
var d1 new DateTime(2008, 10, 1);
var d2 new DateTime(2008, 10, 10);
print(d1.isAfter(d2)); // d1 是否在d2之后
print(d1.isBefore(d2)); // d1 是否在 d2 之前false
truevar d1 new DateTime.now()
var d2 d1.add(new Duration(milliseconds: 30));
print(d1.isAtSameMomentAs(d2));false时间差
var d1 new DateTime(2008, 10, 1);
var d2 new DateTime(2008, 10, 10);
var difference d1.difference(d2);
print([difference.inDays, difference.inHours]);[-9, -216]时间戳
var now new DateTime.now();
print(now.millisecondsSinceEpoch); // 秒级
print(now.microsecondsSinceEpoch); // 毫秒级