@Test public void backToBackQuotedStringsShouldFormSingleToken() { assertEquals(Arrays.asList("foobarbaz"), StringUtils.shellSplit("\"foo\"'bar'baz")); assertEquals(Arrays.asList("three four"), StringUtils.shellSplit("\"three\"' 'four")); }
public static List<String> shellSplit(CharSequence string) { List<String> tokens = new ArrayList<>(); if ( string == null ) { return tokens; } boolean escaping = false; char quoteChar = ' '; boolean quoting = false; StringBuilder current = new StringBuilder() ; for (int i = 0; i<string.length(); i++) { char c = string.charAt(i); if (escaping) { current.append(c); escaping = false; } else if (c == '\\' && !(quoting && quoteChar == '\'')) { escaping = true; } else if (quoting && c == quoteChar) { quoting = false; } else if (!quoting && (c == '\'' || c == '"')) { quoting = true; quoteChar = c; } else if (!quoting && Character.isWhitespace(c)) { if (current.length() > 0) { tokens.add(current.toString()); current = new StringBuilder(); } } else { current.append(c); } } if (current.length() > 0) { tokens.add(current.toString()); } return tokens; }
StringUtils { public static List<String> shellSplit(CharSequence string) { List<String> tokens = new ArrayList<>(); if ( string == null ) { return tokens; } boolean escaping = false; char quoteChar = ' '; boolean quoting = false; StringBuilder current = new StringBuilder() ; for (int i = 0; i<string.length(); i++) { char c = string.charAt(i); if (escaping) { current.append(c); escaping = false; } else if (c == '\\' && !(quoting && quoteChar == '\'')) { escaping = true; } else if (quoting && c == quoteChar) { quoting = false; } else if (!quoting && (c == '\'' || c == '"')) { quoting = true; quoteChar = c; } else if (!quoting && Character.isWhitespace(c)) { if (current.length() > 0) { tokens.add(current.toString()); current = new StringBuilder(); } } else { current.append(c); } } if (current.length() > 0) { tokens.add(current.toString()); } return tokens; } }
StringUtils { public static List<String> shellSplit(CharSequence string) { List<String> tokens = new ArrayList<>(); if ( string == null ) { return tokens; } boolean escaping = false; char quoteChar = ' '; boolean quoting = false; StringBuilder current = new StringBuilder() ; for (int i = 0; i<string.length(); i++) { char c = string.charAt(i); if (escaping) { current.append(c); escaping = false; } else if (c == '\\' && !(quoting && quoteChar == '\'')) { escaping = true; } else if (quoting && c == quoteChar) { quoting = false; } else if (!quoting && (c == '\'' || c == '"')) { quoting = true; quoteChar = c; } else if (!quoting && Character.isWhitespace(c)) { if (current.length() > 0) { tokens.add(current.toString()); current = new StringBuilder(); } } else { current.append(c); } } if (current.length() > 0) { tokens.add(current.toString()); } return tokens; } private  StringUtils(); }
StringUtils { public static List<String> shellSplit(CharSequence string) { List<String> tokens = new ArrayList<>(); if ( string == null ) { return tokens; } boolean escaping = false; char quoteChar = ' '; boolean quoting = false; StringBuilder current = new StringBuilder() ; for (int i = 0; i<string.length(); i++) { char c = string.charAt(i); if (escaping) { current.append(c); escaping = false; } else if (c == '\\' && !(quoting && quoteChar == '\'')) { escaping = true; } else if (quoting && c == quoteChar) { quoting = false; } else if (!quoting && (c == '\'' || c == '"')) { quoting = true; quoteChar = c; } else if (!quoting && Character.isWhitespace(c)) { if (current.length() > 0) { tokens.add(current.toString()); current = new StringBuilder(); } } else { current.append(c); } } if (current.length() > 0) { tokens.add(current.toString()); } return tokens; } private  StringUtils(); @Deprecated static String replace(String string, String oldString, String newString); static String replaceIgnoreCase(String line, String oldString,
                                                 String newString); static String replaceIgnoreCase(String line, String oldString,
            String newString, int[] count); static String replace(String line, String oldString,
            String newString, int[] count); static String stripTags(String in); static String escapeHTMLTags(String in); static String escapeHTMLTags(String in, boolean includeLF); static String hash(String data); static String hash(String data, String algorithm); static String hash(byte[] bytes, String algorithm); static String encodeHex(byte[] bytes); static byte[] decodeHex(String hex); static String encodeBase64(String data); static String encodeBase64(byte[] data); static byte[] decodeBase64(String data); static String encodeBase32(String data); static String encodeBase32(byte[] data); static byte[] decodeBase32(String data); static boolean isBase32(String data); static String[] toLowerCaseWordArray(String text); static String randomString(int length); static String chopAtWord(String string, int length); static String wordWrap(String input, int width, Locale locale); static String escapeForSQL(String string); static String escapeForXML(String string); static String unescapeFromXML(String string); static String zeroPadString(String string, int length); static String dateToMillis(Date date); static String getFullElapsedTime(final long delta); static String getFullElapsedTime(final Duration delta); static String getElapsedTime(long delta); static String getTimeFromLong(long diff); static String collectionToString(Collection<String> collection); static Collection<String> stringToCollection(String string); static boolean contains(String[] array, String item); static String abbreviate(String str, int maxWidth); static boolean isValidEmailAddress(String address); static String validateDomainName(String domain); static String removeXSSCharacters(String input); @Deprecated static byte[] getBytes(String input); @Deprecated static String getString(byte[] input); static boolean containsIgnoringCase(final String nullableString, final String value); static Optional<Integer> parseInteger(final String value); static Optional<Long> parseLong(final String value); static Optional<Double> parseDouble(final String value); static Optional<Boolean> parseBoolean(final String value); static List<String> shellSplit(CharSequence string); }
StringUtils { public static List<String> shellSplit(CharSequence string) { List<String> tokens = new ArrayList<>(); if ( string == null ) { return tokens; } boolean escaping = false; char quoteChar = ' '; boolean quoting = false; StringBuilder current = new StringBuilder() ; for (int i = 0; i<string.length(); i++) { char c = string.charAt(i); if (escaping) { current.append(c); escaping = false; } else if (c == '\\' && !(quoting && quoteChar == '\'')) { escaping = true; } else if (quoting && c == quoteChar) { quoting = false; } else if (!quoting && (c == '\'' || c == '"')) { quoting = true; quoteChar = c; } else if (!quoting && Character.isWhitespace(c)) { if (current.length() > 0) { tokens.add(current.toString()); current = new StringBuilder(); } } else { current.append(c); } } if (current.length() > 0) { tokens.add(current.toString()); } return tokens; } private  StringUtils(); @Deprecated static String replace(String string, String oldString, String newString); static String replaceIgnoreCase(String line, String oldString,
                                                 String newString); static String replaceIgnoreCase(String line, String oldString,
            String newString, int[] count); static String replace(String line, String oldString,
            String newString, int[] count); static String stripTags(String in); static String escapeHTMLTags(String in); static String escapeHTMLTags(String in, boolean includeLF); static String hash(String data); static String hash(String data, String algorithm); static String hash(byte[] bytes, String algorithm); static String encodeHex(byte[] bytes); static byte[] decodeHex(String hex); static String encodeBase64(String data); static String encodeBase64(byte[] data); static byte[] decodeBase64(String data); static String encodeBase32(String data); static String encodeBase32(byte[] data); static byte[] decodeBase32(String data); static boolean isBase32(String data); static String[] toLowerCaseWordArray(String text); static String randomString(int length); static String chopAtWord(String string, int length); static String wordWrap(String input, int width, Locale locale); static String escapeForSQL(String string); static String escapeForXML(String string); static String unescapeFromXML(String string); static String zeroPadString(String string, int length); static String dateToMillis(Date date); static String getFullElapsedTime(final long delta); static String getFullElapsedTime(final Duration delta); static String getElapsedTime(long delta); static String getTimeFromLong(long diff); static String collectionToString(Collection<String> collection); static Collection<String> stringToCollection(String string); static boolean contains(String[] array, String item); static String abbreviate(String str, int maxWidth); static boolean isValidEmailAddress(String address); static String validateDomainName(String domain); static String removeXSSCharacters(String input); @Deprecated static byte[] getBytes(String input); @Deprecated static String getString(byte[] input); static boolean containsIgnoringCase(final String nullableString, final String value); static Optional<Integer> parseInteger(final String value); static Optional<Long> parseLong(final String value); static Optional<Double> parseDouble(final String value); static Optional<Boolean> parseBoolean(final String value); static List<String> shellSplit(CharSequence string);  }