Strings
Raw and Multiline Strings
Dart has two string forms that make awkward text easier to write. A raw string r'...' keeps every backslash literal, so a Windows-style path or a regex pattern reads as-is without doubled backslashes. A triple-quoted string '''...''' spans multiple source lines without explicit \n; each newline in the source becomes a real newline in the value. Once built, both forms work with the usual String methods like .contains and .split.
Program
Play the program to build a raw Windows-style path, build a triple-quoted two-line poem, then check the path, split the poem, and print a summary.
raw_multiline.dart
Replay: real traced execution (multi-file project)
void main() {
var winPath = r'C:\Users\Ada\notes.txt';
var hasUsers = winPath.contains(r'\Users\');
var poem = '''line one
line two''';
var lines = poem.split('\n');
var count = lines.length;
var joined = lines.join(' | ');
print('$hasUsers $count $joined');
}
winPath ← C:\Users\Ada\notes.txt
1void main() {2 var winPath = r'C:\Users\Ada\notes.txt';3 var hasUsers = winPath.contains(r'\Users\');values this stepC:\Users\Ada\notes.txtwinPathhasUsers ← true
2var winPath = r'C:\Users\Ada\notes.txt';3var hasUsers = winPath.contains(r'\Users\');4var poem = '''line onevalues this steptruehasUsersC:\Users\Ada\notes.txtwinPathpoem ← line one\nline two
3 var hasUsers = winPath.contains(r'\Users\');4 var poem = '''line one5line two''';values this stepline one\nline twopoemlines ← [line one, line two]
5line two''';6 var lines = poem.split('\n');7 var count = lines.length;values this step[line one, line two]linesline one\nline twopoemcount ← 2
6var lines = poem.split('\n');7var count = lines.length;8var joined = lines.join(' | ');values this step2count[line one, line two]linesjoined ← line one | line two
7var count = lines.length;8var joined = lines.join(' | ');9print('$hasUsers $count $joined');values this stepline one | line twojoined[line one, line two]linesprint('$hasUsers $count $joined');
8 var joined = lines.join(' | ');9 print('$hasUsers $count $joined');10}outputtrue 2 line one | line twovalues this steptruehasUsers2countline one | line twojoined
Follow the Strings
winPathisC:\Users\Ada\notes.txt..contains(r'\Users\')makeshasUserstrue.- The poem splits into
line oneandline two. countbecomes2.- Joining with
|givesline one | line two, so the output istrue 2 line one | line two. | value | result | | --- | --- | | path check | true | | split lines |line one,line two| | count | 2 | | joined | line one | line two |
raw strings
`r'...'` keeps every backslash literal. A Windows path or regex pattern then reads exactly as written, with no need to double each `\`.
triple-quoted strings
`'''...'''` spans multiple source lines; each newline in the source becomes a real newline in the value, so the string body reads like the original block.
methods are the same
Whatever the literal form, the resulting `String` exposes the same methods: `.contains(needle)` returns a `bool`, `.split(sep)` returns a `List<String>`.
Exercise: raw_multiline.dart
Reproduce true 2 line one | line two, then trace how the raw path check, two split lines, and joined text build that output.