基础数据类型与正则
数字与布尔值
@H_
403_5@数字类型与布尔类型与其他语言是一样一样的
int figureA = -93;
print(figureA.isNegative);
print(figureA.isFinite);
print(figureA.isInfinite);
double figureB = 64.742;
print(figureB.sign);
print(figureB.runtimeType);
print(figureB.hashCode);
int figureC = 13;
print(figureC.isOdd);
print(figureC.isEven);
print(figureC.bitLength);
int figureA = -93;
print(figureA.abs());
print(figureA.toString());
double figureB = 64.742;
print(figureB.toInt());
print(figureB.toDouble());
print(figureB.ceilToDouble());
print(figureB.floorToDouble());
print(figureB.roundToDouble());
print(figureB.toStringAsFixed(2));
print(figureB.toStringAsPrecision(3));
int figureC = 31;
print(figureC.compareTo(20));
print(figureC.clamp(20,25));
print(figureC.toRadixString(16));
int figureD = 12;
print(figureD.gcd(18));
print(figureD.remainder(18));
print(figureD.toStringAsExponential(2));
字符串常量与变量
字符串常量
@H_
403_5@String是一成不变的,一旦定义就不能改变
String name = "XiaoMing say : \n";
String say = """ Keep on going ... never give up ... never say die ... """;
print(name + say);
String str = "Hello world!";
print(str.codeUnits);
print(str.hashCode);
print(str.isEmpty);
print(str.isNotEmpty);
print(str.length);
print(str.runes);
print(str.runtimeType);
@H_
403_5@返回对象的字符串表示
String str = "Hello world!";
print(str.toString());
@H_
403_5@
截取字符串
String str = 'Dart is fun';
String newStr = str.substring(0,4);
print(newStr);
@H_
403_5@在字符串中插入字符串
String name = "XiaoMing";
print("My name is ${name}");
@H_
403_5@
输出字符串的Unicode编码
String str = "Dart";
print(str.codeUnitAt(0));
print(str.codeUnits);
@H_
403_5@去掉字符串前后空格
String str = "\tDart is fun\n";
print(str.trimLeft());
print(str.trimRight());
print(str.trim());
@H_
403_5@字符串的大小写转换
String str = "ABCdef";
print(str.toLowerCase());
print(str.toUpperCase());
@H_
403_5@拆分字符串
String strA = "Hello world!";
print(strA.split(" "));
String strB = "abba";
print(strB.split(new RegExp(r"b*")));
@H_
403_5@是否包含其他字符串
String str = 'Dart strings';
print(str.contains('D'));
print(str.contains(new RegExp(r'[A-Z]')));
print(str.contains('D',0));
print(str.contains(new RegExp(r'[A-Z]'),0));
@H_
403_5@在字符串前后补占位符
String str = "86";
print(str.padLeft(4,'0'));
print(str.padRight(4,'0'));
@H_
403_5@
获取指定字符出现的位置
String str = 'Dartisans';
print(str.indexOf('art'));
print(str.indexOf(new RegExp(r'[A-Z][a-z]')));
print(str.lastIndexOf('a'));
print(str.lastIndexOf(new RegExp(r'a(r|n)')));
@H_
403_5@替换字符串中所有匹配字符
String str = "resume";
print(str.replaceAll(new RegExp(r'e'),'é'));
字符串变量
@H_
403_5@StringBuffer是可改变的,定义后还可以再
修改
StringBuffer xiaomingSaid = new StringBuffer();
xiaomingSaid.write("All the world's a stage ... ");
xiaomingSaid.write("And all the men and women merely players ...");
print(xiaomingSaid);
StringBuffer strBuf = new StringBuffer();
strBuf.write("Sow nothing,reap nothing.");
print(strBuf.hashCode);
print(strBuf.isEmpty);
print(strBuf.isNotEmpty);
print(strBuf.length);
print(runtimeType);
StringBuffer strBuf = new StringBuffer();
strBuf.write("Do one thing at a time,and do well.");
print(strBuf.toString());
strBuf.clear();
正则表达式
RegExp exp = new RegExp(r"(\w+)");
print(exp.hashCode);
print(exp.isCaseSensitive);
print(exp.isMultiLine);
print(exp.pattern);
print(exp.runtimeType);
RegExp exp = new RegExp(r"(\w+)");
print(exp.allMatches("abc def ghi"));
print(exp.firstMatch(""));
print(exp.hasMatch("as"));
print(exp.matchAsPrefix("ab cd",3));
print(exp.stringMatch("abc de"));
print(exp.toString());
实用案例
@H_
403_5@验证邮政编码的正则,返回是否匹配的布尔值
RegExp postalcode = new RegExp(r'(\d{6})');
print(postalcode.hasMatch("518000"));
@H_
403_5@验证手机号码的正则,以Iterable< Match >返回所有匹配项
RegExp mobile = new RegExp(r"(0|86|17951)?(13[0-9]|15[0-35-9]|17[0678]|18[0-9]|14[57])[0-9]{8}");
Iterable<Match> mobiles = mobile.allMatches("13812345678 12345678901 17012345678");
for (Match m in mobiles) {
String match = m.group(0);
print(match);
}
@H_
403_5@验证网址URL的正则,如果匹配成功以Match返回匹配项,否则返回null
RegExp url = new RegExp(r"^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+");
print(url.firstMatch("http://www.google.com"));
@H_
403_5@验证身份证号码的正则,返回第一个匹配的字符串
RegExp identity = new RegExp(r"\d{17}[\d|x]|\d{15}");
print(identity.stringMatch("My id number is 35082419931023527x"));