Strings { public static String[] splitWhitespaces(String s) { if(null == s || s.length() == 0) { return Arrays2.EMPTY_STRING_ARRAY; } List<String> list = new ArrayList<String>(); int i = 0, start = 0, len = s.length(); boolean match = false; while (i < len) { if (Character.isWhitespace(s.charAt(i))) { if (match) { String token = s.substring(start, i).trim(); if (token.length() > 0) { list.add(token); } match = false; } start = ++i; continue; } match = true; i++; } if(match) { String token = s.substring(start, i).trim(); if(token.length() > 0) { list.add(token); } } return list.toArray(new String[list.size()]); } protected  Strings(); static String nullToEmpty(String string); static boolean isEmpty(String string); static boolean isAllEmpty(String... strings); static boolean isNotEmpty(String string); static boolean isBlank(CharSequence string); static boolean isNotBlank(String string); static boolean isNullOrEmpty(Object value); static boolean isNullOrBlank(Object value); static boolean equals(String string1, String string2); static boolean equals(String string1, String string2, boolean ignoreCase); static boolean equalsIgnoreCase(String string1, String string2); static int length(String s); static int count(String str, char c); static String firstNotEmpty(String... strings); static String format(String template, Object... args); static String trim(String string); static String trimStart(String s); static String trimStart(String str, char... trimChars); static String trimEnd(String s); static String trimEnd(String str, char... trimChars); static String trimToNull(String string); static String[] split(String string); static String[] splitCommaOrWhitespaces(String s); static String[] splitWhitespaces(String s); static String[] splitMultiLines(String string); static String[] splitMultiLines(String string,char separator); static String[] split(String string, char separator); static String[] split(String string, char... separators); static String[] split(String string, String separator); static String[] split(String string, char separator, boolean trim); static String[] split(String string, char[] separators, boolean trim); static String[] split(String string, String separator, boolean trim); static String[] split(String string, String separator, boolean trim,boolean ignoreEmpty); static String[] split(String string, char[] separators, boolean trim,boolean ignoreEmpty); static String replaceOnce(String text, String oldString, String newString); static String replace(String text, String oldString, String newString); static String replaceIgnoreCase(String text, String oldString, String newString); static String replace(String text, char oldChar, char newChar); static String remove(String string, String remove); static String remove(String string, char remove); static String removeBlank(String str); static String removeStart(String string, String remove); static String removeStartIgnoreCase(String string, String remove); static String removeEnd(String string, String remove); static String removeEndIgnoreCase(String str, String remove); static String concat(String... strs); static String join(Object[] array, char separator); static String join(Object[] array, String separator); static String join(Object[] array,String separator,boolean ignoreEmpty); static String join(Iterator<?> iterator, char separator); static String join(Iterator<?> iterator, String separator); static String join(Iterable<?> iterable, char separator); static String join(Iterable<?> iterable, String separator); static String upperFirst(String string); static String lowerFirst(String string); static String upperCase(String string); static String lowerCase(String string); static String lowerCamel(String string, char seperator); static String lowerCamel(String... parts); static String upperCamel(String string, char seperator); static String upperCamel(String... parts); static String lowerUnderscore(String name); static String lowerHyphen(String name); static boolean startsWith(String string, String startsWith); static boolean startsWithIgnoreCase(String string, String startsWith); static boolean startsWith(String str, String prefix, boolean ignoreCase); static boolean endsWith(String string, String endsWith); static boolean endsWithIgnoreCase(String string, String endsWith); static boolean endsWith(String str, String suffix, boolean ignoreCase); static boolean contains(String string, char c); static boolean contains(String string, String contains); static boolean containsWhitespaces(CharSequence cs); static boolean containsIgnoreCase(String string,String contains); static int indexOfWhitespace(String s); static int indexOf(String string,String indexOf); static int indexOf(String string,String indexOf,int fromIndex); static int indexOfIgnoreCase(String string,String indexOf); static int indexOfIgnoreCase(String string,String indexOf,int fromIndex); static int lastIndexOf(String string,String indexOf); static int lastIndexOf(String string,String indexOf,int fromIndex); static int lastIndexOfIgnoreCase(String string, String indexOf); static int lastIndexOfIgnoreCase(String string, String indexOf, int fromIndex); static String substringBefore(String string,String indexOf); static String substringAfter(String string,String indexOf); static String left(String string, int len); static String right(String string, int len); static String abbreviate(String str, int maxWidth); static String abbreviate(String str, int maxWidth, String abrevMarker); static String abbreviateMiddle(String str, int maxWidth); static String abbreviateMiddle(String str, String middle, int length); static String abbreviatePrefix(String str, int prefixLength, int length, String abrevMarker); static String repeat(char ch, int repeat); static String repeat(String string, int count); static boolean isDigits(String cs); static boolean isLetters(String s); static boolean isNumber(String str); static String newString(byte[] bytes, String charsetName); static String newStringIso8859_1(byte[] bytes); static String newStringUsAscii(byte[] bytes); static String newStringUtf8(byte[] bytes); static byte[] getBytes(String str, String charset); static byte[] getBytesIso8859_1(String string); static byte[] getBytesUsAscii(String string); static byte[] getBytesUtf8(String string); static final String EMPTY; static final String NULL; static final String COMMA; static final char[] DEFAULT_SPLIT_CHARS; }
@Test public void testSplitWhitespaces() { assertArrayEquals(Strings.splitWhitespaces("1"), new String[]{"1"}); assertArrayEquals(Strings.splitWhitespaces(" 1"), new String[]{"1"}); assertArrayEquals(Strings.splitWhitespaces(" 1"), new String[]{"1"}); assertArrayEquals(Strings.splitWhitespaces(" 1 "), new String[]{"1"}); assertArrayEquals(Strings.splitWhitespaces(" 1 "), new String[]{"1"}); assertArrayEquals(Strings.splitWhitespaces("1 2"), new String[]{"1","2"}); assertArrayEquals(Strings.splitWhitespaces(" 1 2"), new String[]{"1","2"}); assertArrayEquals(Strings.splitWhitespaces(" 1 2 "), new String[]{"1","2"}); assertArrayEquals(Strings.splitWhitespaces(" 1 2 "), new String[]{"1","2"}); assertArrayEquals(Strings.splitWhitespaces(" 1 2 "), new String[]{"1","2"}); }