| 1 | = String usage: split and splitlines = |
---|
| 2 | |
---|
| 3 | ''Part of'' StandardLibraryCategory |
---|
| 4 | |
---|
| 5 | |
---|
| 6 | == Description == |
---|
| 7 | |
---|
| 8 | Exercises std.string.splitlines and std.string.split. |
---|
| 9 | (Note: has simulatedFile to be 'self-contained'. Use commented out lines for real file, assumed to be named tutorial.d) |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | == Example == |
---|
| 13 | |
---|
| 14 | {{{ |
---|
| 15 | #!d |
---|
| 16 | import std.stdio; |
---|
| 17 | import std.string; |
---|
| 18 | |
---|
| 19 | //import std.file; |
---|
| 20 | |
---|
| 21 | //char[] actualFile = cast(char[])std.file.read("tutorial.d"); |
---|
| 22 | |
---|
| 23 | const char[] simulatedFile = |
---|
| 24 | "Line 1 one\n" |
---|
| 25 | "Line 2 two\n" |
---|
| 26 | "Line 3 three"; |
---|
| 27 | |
---|
| 28 | void main () |
---|
| 29 | { |
---|
| 30 | char[][] allLines = splitlines(simulatedFile); |
---|
| 31 | |
---|
| 32 | foreach (char[] curLine; allLines) { |
---|
| 33 | writef(curLine, " = "); |
---|
| 34 | char[][] allWordsInCurLine = split(curLine); |
---|
| 35 | foreach (char[] curWord; allWordsInCurLine) { |
---|
| 36 | writef('[', curWord, ']', ' '); |
---|
| 37 | } |
---|
| 38 | writefln(); |
---|
| 39 | } |
---|
| 40 | } |
---|
| 41 | }}} |
---|
| 42 | |
---|
| 43 | == Source == |
---|
| 44 | |
---|
| 45 | || Link || http://www.dsource.org/tutorials/index.php?show_example=120 || |
---|
| 46 | || Posted by || Lynn || |
---|
| 47 | || Date/Time || Fri Sep 24, 2004 3:15 pm || |