Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static String[] parseLine(String string) {
- ArrayList<String> returnArgs = new ArrayList<String>();
- boolean inQuote = false;
- boolean isEscaped = false;
- StringBuilder curr = new StringBuilder();
- for (char s : string.toCharArray()) {
- String chr = String.valueOf(s);
- if (isEscaped) {
- curr.append(chr);
- isEscaped=false;
- } else {
- if (chr.equalsIgnoreCase("\\")) {
- isEscaped=true;
- } else if (chr.equalsIgnoreCase(" ")) {
- if (inQuote) {
- curr.append(" ");
- } else {
- if (curr.toString().replace(" ", "").length()>0) {
- returnArgs.add(curr.toString().trim());
- }
- curr = new StringBuilder();
- }
- } else if (chr.equalsIgnoreCase("\"")) {
- inQuote = !inQuote;
- } else {
- curr.append(chr);
- }
- }
- }
- returnArgs.add(curr.toString());
- String[] rtrn = new String[returnArgs.size()];
- return returnArgs.toArray(rtrn);
- }
Add Comment
Please, Sign In to add comment