public static String[] splitlines(String text) { ArrayList<String> ret = new ArrayList<String>(); int p = 0; while (true) { int p2 = text.indexOf('\n', p); if (p2 < 0) { ret.add(text.substring(p)); break; } ret.add(text.substring(p, p2)); p = p2 + 1; } return (ret.toArray(new String[0])); }
public static String[] splitwords(String text) { ArrayList<String> words = new ArrayList<String>(); StringBuilder buf = new StringBuilder(); String st = "ws"; int i = 0; while (i < text.length()) { char c = text.charAt(i); if (st == "ws") { if (!Character.isWhitespace(c)) st = "word"; else i++; } else if (st == "word") { if (c == '"') { st = "quote"; i++; } else if (c == '\\') { st = "squote"; i++; } else if (Character.isWhitespace(c)) { words.add(buf.toString()); buf = new StringBuilder(); st = "ws"; } else { buf.append(c); i++; } } else if (st == "quote") { if (c == '"') { st = "word"; i++; } else if (c == '\\') { st = "sqquote"; i++; } else { buf.append(c); i++; } } else if (st == "squote") { buf.append(c); i++; st = "word"; } else if (st == "sqquote") { buf.append(c); i++; st = "quote"; } } if (st == "word") words.add(buf.toString()); if ((st != "ws") && (st != "word")) return (null); return (words.toArray(new String[0])); }