Dart入门-基础数据类型-字符串常量与变量
2019-01-12 本文已影响14人
RobinZhao
void stringDemo() {
// String 不可变
String name = 'XiaoHong say:\n';
String say = """
Keep going...
never give up...
never say die...
""";
print(name + say);
stringPropertyPart();
stringCommonMethod();
stringBufferDemo();
}
void stringPropertyPart() {
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);
}
void stringCommonMethod() {
String strA = 'Hello World!';
// 返回对象的字符串表示形式
print(strA.toString());
// 1、截取字符串
String strB = 'Dart is funny';
// 返回此字符串的子字符串,该字符串从[startIndex](包括)延伸到[endIndex],不包括。
String strBNew = strB.substring(0,2); // Da
print(strBNew);
// 返回此字符串的子字符串,该字符串从[startIndex](包括)延伸到结束。
String strBaNew = strB.substring(1); // art is funny
print(strBaNew);
// 2. 在字符串中插入字符串
String strc = 'Dart is funny';
print('I think $strc');
// print('I think ${strc}');
// 3. 输出字符串的Unicode编码
String strd = 'Dart is funny';
// 返回给定[index]处的16位UTF-16代码单元
print(strd.codeUnitAt(0));
// 返回此字符串的UTF-16代码单元的不可修改列表
print(strd.codeUnits);
// 4. 去掉字符串前后空格
String stre = "\tDart is funny\n";
print(stre.trimLeft());
print(stre.trimRight());
// 头尾全部去掉
print(stre.trim());
// 5. 字符串的大小写转换
String strf = "ABCdef";
print(strf.toLowerCase());
print(strf.toUpperCase());
// 6.是否包含其他字符串
String strg = 'Dart strings';
// contains(Pattern other, [int startIndex = 0]) → bool
// 如果提供了[startIndex],则此方法仅在该索引处或之后匹配
// [startIndex]不得为负数或大于[length]。
print(strg.contains('D'));
print(strg.contains(RegExp(r'[A-Z]')));
print(strg.contains('D', 1));
print(strg.contains(RegExp(r'[A-Z]'), 0));
// 7.在字符串前后补占位符
String strh = "86";
// padLeft(int width, [String padding = ' ']) → String
/**如果字符串比[宽度]短,则在左侧填充此字符串。
* 对于长度小于[width]的每个位置,返回一个新的字符串,该字符串将[padding]预先填充到此字符串上一次。
* 如果[width]已经小于或等于this.length,则不添加填充。 负宽度被视为零。
* 如果[padding]的长度不同于1,则结果将不具有长度宽度。 这可能适用于填充是表示单个字符的较长字符串的情况,例如“ ” 或“\ u {10002}”。 在这种情况下,用户应确保this.length是字符串长度的正确度量。
* */
print(strh.padLeft(4, '00'));
// padRight(int width, [String padding = ' ']) → String
print(strh.padRight(4, '00'));
// 8.替换字符串中所有匹配字符
String stri = "resume";
print(stri.replaceAll(new RegExp(r'e'), 'é'));
// '0.0001'.replaceFirst(new RegExp(r'0'), ''); // '.0001'
// '0.0001'.replaceFirst(new RegExp(r'0'), '7', 1); // '0.7001'
// print(stri.replaceFirst(from, to))
}
void stringBufferDemo() {
// 可变字符串
StringBuffer xiaomingSaid = new StringBuffer();
// 将[obj]的内容(转换为字符串)添加到缓冲区。
xiaomingSaid.write("All the world's a stage ... ");
xiaomingSaid.write("And all the men and women merely players ...");
print(xiaomingSaid);
// 返回字符串缓冲区的哈希码
print(xiaomingSaid.hashCode);
// 字符串缓冲区是否为空
print(xiaomingSaid.isEmpty);
// 字符串缓冲区是否不为空
print(xiaomingSaid.isNotEmpty);
// 返回字符串缓冲区累积内容的长度
print(xiaomingSaid.length);
// 返回对象运行时的类型
print(xiaomingSaid.runtimeType);
// 返回字符串缓冲区的所有内容
print(xiaomingSaid.toString());
// 清除字符串缓冲区
xiaomingSaid.clear();
print('clear:$xiaomingSaid');
}