标签:ring bool 字符串替换 indexof not bst 第三章 case eal
//声明字符串
String str1 = ‘hello‘;
String str2 = ‘dart‘;
//字符串拼接
print(‘$str1 $str2‘);
print(str1 + " " + str2);
String str = ‘hello world‘;
print(str.length); //返回字符长度
print(str.runtimeType); //返回当前数据类型
print(str.isEmpty); //返回当前字符串是否为空
print(str.isNotEmpty); //返回当前字符串是否不为空
print(str.codeUnits); //字符串的UTF-16代码单元列表
print(str.hashCode); //返回字符串的哈希码,==匹配使用的是哈希码
print(str.runes); //Rune是UTF-32编码的字符串,用来定义表情等
/*输出----------------------------------------
11
String
false
true
[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
1045060183
(104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100)
*/
String str =‘ hello world ‘;
//trim()去掉首尾空格
print(str.trim()); //hello world
//trimRight()去掉右边空格
print(str.trimRight()); // hello world
//trimLeft()去掉左边边空格
print(str.trimLeft()); //hello world
String str1= ‘hello world‘;
String str2= ‘ABC‘;
//字符串转换成大写
print(str1.toUpperCase()); //HELLO WORLD
//字符串转换成小写
print(str2.toLowerCase()); //abc
String str = ‘hello world‘;
//replaceFirst() 第一个匹配,只替换匹配到的第一个值
//String replaceFirst(Pattern from, String to, [int startIndex])
print(str.replaceFirst(new RegExp(r‘l‘), ‘8‘)); //he8lo world
print(str.replaceFirst(new RegExp(r‘l‘), ‘8‘, 8)); //hello wor8d
//replaceAll() 全部匹配,替换所有匹配到的值
//String replaceAll(Pattern from, String replace)
print(str.replaceAll(new RegExp(‘l‘), ‘8‘)); //he88o wor8d
//replaceRange() 范围替换,把范围内的值去掉,然后插入secondvalue
//String replaceRange(int start, int end, String replacement)
print(str.replaceRange(3, 5, ‘888‘)); //hel888 world
//replaceFirstMapped() 函数第一匹配
//String replaceFirstMapped(Pattern from, String Function(Match) replace, [int startIndex])
print(str.replaceFirstMapped(RegExp(r‘\w‘), (Match m) => ‘${m[0] * 3}‘, 2));//hellllo world
//replaceAllMapped() 函数全部匹配
//String replaceAllMapped(Pattern from, String Function(Match) replace)
print(str.replaceAllMapped(‘o‘, (Match m) => ‘${m[0] * 2}‘)); //helloo woorld
//splitMapJoin() 函数匹配替换,非匹配替换
//String splitMapJoin(Pattern pattern, {String Function(Match) onMatch, String Function(String) onNonMatch})
print(str.splitMapJoin(
‘ll‘,
onMatch: (match) => ‘${match.group(0) * 2}‘,//匹配到的值*2
onNonMatch: (nonMatch) => ‘8‘,//没被匹配的值替换成8
)); //8llll8
String str = ‘hello world‘;
//startsWith() 检测字符串开头
//bool startsWith(Pattern pattern, [int index])
print(str.startsWith(‘h‘)); //true
print(str.startsWith(‘e‘, 1)); //true
//endsWith() 检测字符串结尾
// bool endsWith(String other)
print(str.endsWith(‘d‘)); //true
//contains() 检测字符串是否包含关键字
//bool contains(Pattern other, [int startIndex])
print(str.contains(‘world‘)); //true
print(str.contains(‘hello‘, 5));//false
String str = ‘hello world‘;
//padLeft() padRight() 字符串的长度 < width, 向左补充secondvalue,默认空格
//String padLeft(int width, [String padding])
print(str.padLeft(15)); // hello world
print(str.padLeft(15, ‘0‘)); //0000hello world
//padRight()以上同理
print(str.padRight(15)); //hello world
print(str.padRight(15, ‘0‘)); //hello world0000
String str = ‘hello world‘;
//split() 以firstvalue分割字符串,返回一个数组
print(str.split(" ")); //[hello, world]
//substring()
//String substring(int startIndex, [int endIndex])
print(str.substring(1, 5)); //ello
String str = ‘hello world‘;
//indexOf() 获取关键字的索引值(下标)
//int indexOf(Pattern pattern, [int start])
print(str.indexOf(‘l‘)); //2
print(str.indexOf(‘l‘, 5)); //9
//lastIndexOf() 返回字符串最后一个关键词的索引值
//int lastIndexOf()(Pattern pattern, [int start])
print(str.lastIndexOf(‘l‘)); //9
String str = ‘hello world‘;
//codeUnitAt() //返回相应索引的UTF-16编码
//int codeUnitAt(int index)
print(str.codeUnitAt(2)); //108
String str = ‘hello world‘;
//compareTo() 当前对象值大于指定值:1 || 当前对象值小于指定值:1 || 值相等:0
//int compareTo(String other)
print(str.compareTo(‘hello‘)); //1
print(str.compareTo(‘hello world‘)); //0
print(str.compareTo(‘hello world2222‘)); //-1
标签:ring bool 字符串替换 indexof not bst 第三章 case eal
原文地址:https://www.cnblogs.com/TobuTobu/p/14350685.html