乐趣区

dart字符串基本属性及方法

代码示例

// 单行字符串
String str = 'abc';

// 多行字符串
String multiLine = ''' 这是多行文本
    能够回车的
''';
// 字符串连贯形式
'Dart' 'is' 'fun!'; // 'Dart is fun!'
'Dart' + 'is' + 'fun!'; // 'Dart is fun!'
'Dart' * 2; // 'DartDart'
String 属性
属性 形容
List<int> codeUnits 获取字符串 utf-16 编码值的列表
int hashCode 获取字符派生的哈希代码
bool isEmpty 字符串是否为空
bool isNotEmpty 字符串是否不为空
int length 获取字符串长度
Runes runes 获取字符串 utf-16 编码值的可迭代列表
Type runtimeType 获取运行时的数据类型
String 办法
属性 形容
int codeUnitAt(int index) 返回给定索引值的对应 utf-16 编码
int compareTo(String other) 与传入字符串进行比拟, 有雷同返回 1,否则返回 -1
bool contains(Pattern other, [int index]) 查找返回字符串是否有符号要求的,传入 index 规定从 index 位开始查找
bool endsWith(String other) 字符串的结尾是否为传入的值
int indexOf(Pattern other, [int start]) 从字符串后面开始查找返回合乎规定的第一个的索引地位,传入 start 规定从哪里开始查找
int lastIndexOf(Pattern other, [int start]) 与 indexOf 雷同,不同的是这个办法是从前面开始查找
String padLeft(int width, [String padding]) 如果字符串没有 width 的长度,则在后面加上 padding 字符串并返回, 不会扭转原字符串
String padRight(int width, [String padding]) padLeft 办法雷同,不同的是从 padding 加在前面
String replaceAll(Pattern from, String to) 替换所有匹配的子字符串
String replaceAllMapped(Pattern from, String replace(match)) 将匹配到的字符串用函数解决后返回字符串替换
String replaceFirst(Pattern from, String to, [int index]) 替换第一个匹配的子字符串, 能够规定从 index 出开始匹配
String replaceFirstMapped(Pattern from, String replace(match), [int index]) replaceAllMapped,此办法只替换第一个匹配的值,能够规定从index 处开始匹配
String replaceRange(int start, int end, String to) 替换范畴类的字符串
List<String> split(Pattern pattern) 拆分字符串为数组
String splitMapJoin(Pattern pattern, {String onMatch(match), String onNonMatch(match) }) 拆分替换字符串, 匹配和不匹配的执行对应函数替换成返回值
bool startsWith(Pattern pattern, [int index]) 是否是匹配的正则或字符串开始,能够规定从 index 开始查找
String substring(int startIndex, [int endIndex]) 提取字符串中 startIndex(蕴含) 到endIndex(不蕴含)两个指定的索引号之间的字符。
String toLowerCase() 把字符串转换为小写
String toUpperCase() 把字符串转换为大写
String trim() 去除字符串两边的空白
String trimLeft() 去除字符串右边的空白
String trimRight() 去除字符串左边的空白
退出移动版