声明字符串(String)
(1)、单行字符串:单引号、双引号都可以
(2)、多行字符串:三个引号可以声明包含字符串的字符串
正则表达式
(1)、RegExp(r’正则表达式’)
(2)、RegExp(r’/d+’)
void main() {
// 声明字符串
var str1 = 'Hello world'; // 单引号
var str2 = "Hello Dart"; // 双引号
String str3 = 'Hi World'; // 单引号
String str4 = "Hi Dart"; // 双引号
print('$str1, $str2, $str3, $str4');
// 通过三个引号声明字符串
String str5 = '''字符串
拼接
''';
print(str5);
String str6 = """----
Dart
Flutter
""";
print(str6);
/* 常见操作 */
// 字符串是不可以修改的,常见的字符串操作都会返回一个新的字符串
// 1、字符串拼接
print(str1 + str2);
print('$str1 $str2');
// 获取字符串长度
print('str1 has ${str1.length} letters'); // str1 has 11 letters
print(str1[0]); // H
print('dart' == 'dart'); // true
print('dart'.compareTo('dart')); // 0
// 2、字符串的分割
print(str1.split(',')); // [Hello world]
// 3、去除首尾空格
print(' a bc '.trim()); // a bc
// 4、判断字符串是否为空
print(''.isEmpty); // true
print(''.isNotEmpty); // false
// 5、字符串替换
print(str1.replaceAll('world', '世界')); // Hello 世界
// 支持正则替换
print('j1h2j3h4ppp'.replaceAll(RegExp(r'/d+'), '-')); // j-h-j-h-ppp
// 6、通过正则匹配手机号
var isPhone = RegExp(r'^1/d{10}$');
print(isPhone.hasMatch('13423459878')); // true
print(isPhone.hasMatch('1234567892')); // false
// 7、查找字符串
print(str1.contains('h')); // false
print(str1.endsWith('d')); // true
print(str1.startsWith('H')); // true
// 8、定位字符串
print(str1.indexOf('h')); // -1
print(str1.indexOf('H')); // 0
// 9、转换大小写
print('daRt'.toLowerCase()); // dart
print('daRt'.toUpperCase()); // DART
// 10、子字符串,含头不含尾
print('dart'.substring(0, 3)); // dar
}
原创文章,作者:254126420,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/246058.html