Skip to content

Instantly share code, notes, and snippets.

@graphicbeacon
Last active August 8, 2018 21:58
Show Gist options
  • Save graphicbeacon/abea509cc44de22b5c714c686b17c2ce to your computer and use it in GitHub Desktop.
Save graphicbeacon/abea509cc44de22b5c714c686b17c2ce to your computer and use it in GitHub Desktop.
Sample code for "Top 10 String utility methods you should know (Dart) 🎯" (Complete solution)
void main() async {
var str1 = 'Lorem';
var str2 = '$str1 ipsum'; // String interpolation
var str3 = '''Multi
Line
$str1 $str2'''; // Multi line string
print(str1.contains('rem')); // true
print(str2.startsWith('Lorem')); // true
print(str3.startsWith('Noorem')); // false
print(str3.endsWith('ipsum')); // true
print(str3.endsWith(r'$str2')); // false
print(str1.toLowerCase()); // lorem
print(str1.toUpperCase()); // LOREM
print(str3.split('\n')); // ['Multi', 'Line', 'Lorem Lorem ipsum']
print(str3.splitMapJoin(RegExp(r'^', multiLine: true), // Matches the beginning of each line
onMatch: (m) => '**${m.group(0)} ', // Adds asterisk to the line beginning
onNonMatch: (n) => n)); // Leaves non-matches as is
print(str3.indexOf('rem')); // 4
print(str3.lastIndexOf('rem')); // 19
print(str3.lastIndexOf('rem', 18)); // 13
print(' $str2 '.trim()); // 'Lorem ipsum'
print(str1.padLeft(8, 'x')); // xxLorem
print(str1.padRight(8, 'x')); // Loremxx
print(str1.replaceAll('e', 'é')); // Lorém
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment