@Test public void split2() { List<String> tokens = parser.split("(a and b)"); assertEquals(Arrays.asList("(", "a", "and", "b", ")"), tokens); }
public List<String> split(String in) { StringBuilder result = new StringBuilder(); char[] chars = in.toCharArray(); for (int i = 0; i < chars.length; i++) { char c = chars[i]; if (charOperators.contains(String.valueOf(c))) { if (i < chars.length - 2 && charOperators.contains(String.valueOf(chars[i + 1])) && !("(".equals(String.valueOf(chars[i + 1])) || ")".equals(String.valueOf(chars[i + 1])))) { result.append(" ").append(c).append(chars[i + 1]).append(" "); i++; } else { result.append(" ").append(c).append(" "); } } else { result.append(c); } } String[] tokens = result.toString().split(SPLIT_EXPRESSION); List<String> list = new ArrayList<String>(); for (int i = 0; i < tokens.length; i++) { tokens[i] = tokens[i].trim(); if (!tokens[i].equals("")) { list.add(tokens[i]); } } return list; }
Parser { public List<String> split(String in) { StringBuilder result = new StringBuilder(); char[] chars = in.toCharArray(); for (int i = 0; i < chars.length; i++) { char c = chars[i]; if (charOperators.contains(String.valueOf(c))) { if (i < chars.length - 2 && charOperators.contains(String.valueOf(chars[i + 1])) && !("(".equals(String.valueOf(chars[i + 1])) || ")".equals(String.valueOf(chars[i + 1])))) { result.append(" ").append(c).append(chars[i + 1]).append(" "); i++; } else { result.append(" ").append(c).append(" "); } } else { result.append(c); } } String[] tokens = result.toString().split(SPLIT_EXPRESSION); List<String> list = new ArrayList<String>(); for (int i = 0; i < tokens.length; i++) { tokens[i] = tokens[i].trim(); if (!tokens[i].equals("")) { list.add(tokens[i]); } } return list; } }
Parser { public List<String> split(String in) { StringBuilder result = new StringBuilder(); char[] chars = in.toCharArray(); for (int i = 0; i < chars.length; i++) { char c = chars[i]; if (charOperators.contains(String.valueOf(c))) { if (i < chars.length - 2 && charOperators.contains(String.valueOf(chars[i + 1])) && !("(".equals(String.valueOf(chars[i + 1])) || ")".equals(String.valueOf(chars[i + 1])))) { result.append(" ").append(c).append(chars[i + 1]).append(" "); i++; } else { result.append(" ").append(c).append(" "); } } else { result.append(c); } } String[] tokens = result.toString().split(SPLIT_EXPRESSION); List<String> list = new ArrayList<String>(); for (int i = 0; i < tokens.length; i++) { tokens[i] = tokens[i].trim(); if (!tokens[i].equals("")) { list.add(tokens[i]); } } return list; } Parser(); }
Parser { public List<String> split(String in) { StringBuilder result = new StringBuilder(); char[] chars = in.toCharArray(); for (int i = 0; i < chars.length; i++) { char c = chars[i]; if (charOperators.contains(String.valueOf(c))) { if (i < chars.length - 2 && charOperators.contains(String.valueOf(chars[i + 1])) && !("(".equals(String.valueOf(chars[i + 1])) || ")".equals(String.valueOf(chars[i + 1])))) { result.append(" ").append(c).append(chars[i + 1]).append(" "); i++; } else { result.append(" ").append(c).append(" "); } } else { result.append(c); } } String[] tokens = result.toString().split(SPLIT_EXPRESSION); List<String> list = new ArrayList<String>(); for (int i = 0; i < tokens.length; i++) { tokens[i] = tokens[i].trim(); if (!tokens[i].equals("")) { list.add(tokens[i]); } } return list; } Parser(); List<String> toPrefix(String in); List<String> split(String in); }
Parser { public List<String> split(String in) { StringBuilder result = new StringBuilder(); char[] chars = in.toCharArray(); for (int i = 0; i < chars.length; i++) { char c = chars[i]; if (charOperators.contains(String.valueOf(c))) { if (i < chars.length - 2 && charOperators.contains(String.valueOf(chars[i + 1])) && !("(".equals(String.valueOf(chars[i + 1])) || ")".equals(String.valueOf(chars[i + 1])))) { result.append(" ").append(c).append(chars[i + 1]).append(" "); i++; } else { result.append(" ").append(c).append(" "); } } else { result.append(c); } } String[] tokens = result.toString().split(SPLIT_EXPRESSION); List<String> list = new ArrayList<String>(); for (int i = 0; i < tokens.length; i++) { tokens[i] = tokens[i].trim(); if (!tokens[i].equals("")) { list.add(tokens[i]); } } return list; } Parser(); List<String> toPrefix(String in); List<String> split(String in);  }