標籤:
基礎資料類型與正則數字與布爾值
數字類型與布爾類型與其他語言是一樣一樣的
部分屬性
int figureA = -93; // figureA是否為負數 print(figureA.isNegative); // figureA是否是有限的 print(figureA.isFinite); // figureA是否正無窮大或負無窮大 print(figureA.isInfinite); double figureB = 64.742; // 返回figureB的符號,-1.0:值小於0、+1.0:值大於0、-0.0/0.0/NaN:值是其本身 print(figureB.sign); // 返回figureB運行時的類型 print(figureB.runtimeType); // 返回figureB的雜湊碼 print(figureB.hashCode); int figureC = 13; // figureC是否為奇數 print(figureC.isOdd); // figureC是否為偶數 print(figureC.isEven); // 返回figureC所佔儲存位 print(figureC.bitLength);
常用方法
int figureA = -93; // 返回figureA的絕對值 print(figureA.abs()); // 返回figureA的字串 print(figureA.toString()); double figureB = 64.742; // 返回figureB的整數值 print(figureB.toInt()); // 返回figureB的雙精確度值 print(figureB.toDouble()); // 返回大於figureB的雙精確度值 print(figureB.ceilToDouble()); // 返回小於figureB的雙精確度值 print(figureB.floorToDouble()); // 返回figureB四捨五入的雙精確度值 print(figureB.roundToDouble()); // 返回figureB保留幾位小數的字串 print(figureB.toStringAsFixed(2)); // 返回figureB保留幾位小數後精確結果的字串 print(figureB.toStringAsPrecision(3)); int figureC = 31; // figureC對比其他整數,0:相同、1:大於、-1:小於 print(figureC.compareTo(20)); // 將figureC控制在指定區間的整數 print(figureC.clamp(20, 25)); // 返回figureC轉換成指定基數(進位)的字串 print(figureC.toRadixString(16)); int figureD = 12; // 返回figureD與其他整數的最大公約數 print(figureD.gcd(18)); // 返回figureDg與其他整數的截取餘數 print(figureD.remainder(18)); // 返回figureD幾次冪值的字串 print(figureD.toStringAsExponential(2));
字串常量與變數字串常量
String是一成不變的,一旦定義就不能改變
String name = "XiaoMing say : \n";String say = """Keep on going ...never give up ...never say die ...""";print(name + say);
部分屬性
String str = "Hello world!"; // 返回字串的UTF-16代碼單元列表 print(str.codeUnits); // 返回根據代碼單元產生的雜湊碼 print(str.hashCode); // 字串是否為空白 print(str.isEmpty); // 字串是否不為空白 print(str.isNotEmpty); // 字串的長度 print(str.length); // 返回字串Unicode代碼的可迭代對象 print(str.runes); // 返回對象運行時的類型 print(str.runtimeType);
常用方法
返回對象的字串表示
String str = "Hello world!";print(str.toString());
截取字串
String str = ‘Dart is fun‘;String newStr = str.substring(0, 4);print(newStr);
在字串中插入字串
String name = "XiaoMing";print("My name is ${name}");
輸出字串的Unicode編碼
String str = "Dart";print(str.codeUnitAt(0));print(str.codeUnits);
去掉字串前後空格
String str = "\tDart is fun\n";print(str.trimLeft());print(str.trimRight());print(str.trim());
字串的大小寫轉換
String str = "ABCdef";print(str.toLowerCase());print(str.toUpperCase());
拆分字串
String strA = "Hello world!";print(strA.split(" "));String strB = "abba";print(strB.split(new RegExp(r"b*")));
是否包含其他字串
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));
在字串前後補預留位置
String str = "86";print(str.padLeft(4, ‘0‘));print(str.padRight(4, ‘0‘));
擷取指定字元出現的位置
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)‘)));
替換字串中所有匹配字元
String str = "resume";print(str.replaceAll(new RegExp(r‘e‘), ‘é‘));
字串變數
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();
Regex部分屬性
RegExp exp = new RegExp(r"(\w+)"); // 返回Regex的雜湊碼 print(exp.hashCode); // Regex是否區分大小寫 print(exp.isCaseSensitive); // Regex是否匹配多行 print(exp.isMultiLine); // 返回源Regex字串 print(exp.pattern); // 返回對象運行時的類型 print(exp.runtimeType);
常用方法
RegExp exp = new RegExp(r"(\w+)"); // 返回Regex匹配項的可迭代對象 print(exp.allMatches("abc def ghi")); // 搜尋並返回第一個匹配項,沒有則返回null print(exp.firstMatch("")); // Regex是否找到匹配項 print(exp.hasMatch("as")); // 從第幾個字元開始匹配Regex print(exp.matchAsPrefix("ab cd", 3)); // 返回Regex的第一個匹配字串 print(exp.stringMatch("abc de")); // 返回Regex的字串表示 print(exp.toString());
實用案例
驗證郵遞區號的正則,返回是否匹配的布爾值
RegExp postalcode = new RegExp(r‘(\d{6})‘);print(postalcode.hasMatch("518000"));
驗證手機號碼的正則,以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);}
驗證網址URL的正則,如果匹配成功以Match返回匹配項,否則返回null
RegExp url = new RegExp(r"^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+");print(url.firstMatch("http://www.google.com"));
驗證社會安全號碼碼的正則,返回第一個匹配的字串
RegExp identity = new RegExp(r"\d{17}[\d|x]|\d{15}");print(identity.stringMatch("My id number is 35082419931023527x"));
Dart入門—基礎類型與正則