1 | public static List<String> toLines(String s) { |
2 | List<String> lines = new ArrayList<String>(); |
3 | int start = 0; |
4 | while (true) { |
5 | int i = toLines_nextLineBreak(s, start); |
6 | if (i < 0) { |
7 | if (s.length() > start) lines.add(s.substring(start)); |
8 | break; |
9 | } |
10 | |
11 | lines.add(s.substring(start, i)); |
12 | if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n') |
13 | i += 2; |
14 | else |
15 | ++i; |
16 | |
17 | start = i; |
18 | } |
19 | return lines; |
20 | } |
21 | |
22 | private static int toLines_nextLineBreak(String s, int start) { |
23 | for (int i = start; i < s.length(); i++) { |
24 | char c = s.charAt(i); |
25 | if (c == '\r' || c == '\n') |
26 | return i; |
27 | } |
28 | return -1; |
29 | } |
Snippet is not live.
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #2000326 |
Snippet name: | toLines (function) |
Eternal ID of this version: | #2000326/1 |
Text MD5: | fb1e5cc458e3ef3145f0c15849164e29 |
Author: | stefan |
Category: | javax |
Type: | New Tinybrain snippet |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-04-28 20:43:24 |
Source code size: | 737 bytes / 29 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 594 / 3191 |
Referenced in: | [show references] |