我有这个方法,我想知道是否有一种实用且干净的方法来缩短这段代码: 这个练习来自Dart Apprentice: Beyond the Basics 一本书。
void main() {
final lyric = '[00:12.34]Row, row, row your boat';
final openBracketIndex = lyric.indexOf('[');
final colonIndex = lyric.indexOf(':');
final dotIndex = lyric.indexOf('.');
final endBracketIndex = lyric.indexOf(']');
final buffer = StringBuffer();
buffer.write('minutes: ');
buffer.write(lyric.substring(openBracketIndex + 1, colonIndex));
buffer.write('\n');
buffer.write('seconds: ');
buffer.write(lyric.substring(colonIndex + 1, dotIndex));
buffer.write('\n');
buffer.write('hundredths: ');
buffer.write(lyric.substring(dotIndex + 1, endBracketIndex));
buffer.write('\n');
buffer.write('lyrics: ');
buffer.write(lyric.substring(endBracketIndex + 1));
print(buffer);
}
输出为:
minutes: 00
seconds: 12
hundredths: 34
lyrics: Row, row, row your boat
您可以使用级联符号和
writeln
方法将代码缩短为:
void main() {
final lyric = '[00:12.34]Row, row, row your boat';
final openBracketIndex = lyric.indexOf('[');
final colonIndex = lyric.indexOf(':');
final dotIndex = lyric.indexOf('.');
final endBracketIndex = lyric.indexOf(']');
final buffer = StringBuffer()
..write('minutes: ')
..writeln(lyric.substring(openBracketIndex + 1, colonIndex))
..write('seconds: ')
..writeln(lyric.substring(colonIndex + 1, dotIndex))
..write('hundredths: ')
..writeln(lyric.substring(dotIndex + 1, endBracketIndex))
..write('lyrics: ')
..write(lyric.substring(endBracketIndex + 1));
print(buffer);
}
如果我们允许我们在
StringBuilder
之外构建一些字符串,我们也可以进一步缩短它:
void main() {
final lyric = '[00:12.34]Row, row, row your boat';
final openBracketIndex = lyric.indexOf('[');
final colonIndex = lyric.indexOf(':');
final dotIndex = lyric.indexOf('.');
final endBracketIndex = lyric.indexOf(']');
final buffer = StringBuffer()
..writeln('minutes: ${lyric.substring(openBracketIndex + 1, colonIndex)}')
..writeln('seconds: ${lyric.substring(colonIndex + 1, dotIndex)}')
..writeln('hundredths: ${lyric.substring(dotIndex + 1, endBracketIndex)}')
..write('lyrics: ${lyric.substring(endBracketIndex + 1)}');
print(buffer);
}
一个不太漂亮的解决方案是跳过
StringBuffer
,而是使用多行字符串作为模板:
void main() {
final lyric = '[00:12.34]Row, row, row your boat';
final openBracketIndex = lyric.indexOf('[');
final colonIndex = lyric.indexOf(':');
final dotIndex = lyric.indexOf('.');
final endBracketIndex = lyric.indexOf(']');
final string = '''
minutes: ${lyric.substring(openBracketIndex + 1, colonIndex)}
seconds: ${lyric.substring(colonIndex + 1, dotIndex)}
hundredths: ${lyric.substring(dotIndex + 1, endBracketIndex)}
lyrics: ${lyric.substring(endBracketIndex + 1)}
''';
print(string);
}
虽然我通常不是第一个推荐正则表达式的人,但这看起来像是您应该使用正则表达式的一个杰出示例:解析已知的固定格式并提取部分。
并使用
writeln
添加换行符。
final lineRE = RegExp(r"^\[(\d+):(\d+)\.(\d+)\](.*)$");
void main() {
final lyric = '[00:12.34]Row, row, row your boat';
var parts = lineRE.firstMatch(lyric)!;
return (StringBuffer()
..writeln('minutes: ${parts[1]}')
..writeln('seconds: ${parts[2]}')
..writeln('hundredths: ${parts[3]}')
..write('lyrics: ${parts[4]}')
).toString();
}
您可以使用级联符号来使用 StringBuffer 对象,以及 writeAll 语法:
void main() {
final lyric = '[00:12.34]Row, row, row your boat';
final openBracketIndex = lyric.indexOf('[');
final colonIndex = lyric.indexOf(':');
final dotIndex = lyric.indexOf('.');
final endBracketIndex = lyric.indexOf(']');
final buffer = StringBuffer()
..writeAll([
'minutes: ',
lyric.substring(openBracketIndex + 1, colonIndex),
'\n',
'seconds: ',
lyric.substring(colonIndex + 1, dotIndex),
'\n',
'hundredths: ',
lyric.substring(dotIndex + 1, endBracketIndex),
'\n',
'lyrics: ',
lyric.substring(endBracketIndex + 1)]);
print(buffer);
}