@Test public void santizeWhitespace() { assertEquals("", Strings.sanitizeWhitespace(" ")); assertEquals("a", Strings.sanitizeWhitespace(" a ")); assertEquals("ab", Strings.sanitizeWhitespace(" a\nb ")); assertEquals("ab", Strings.sanitizeWhitespace(" a\n b ")); assertEquals("ab", Strings.sanitizeWhitespace("a\n b")); assertEquals("ab c", Strings.sanitizeWhitespace("a\n b c")); assertEquals("ab c", Strings.sanitizeWhitespace("a\n b c")); assertEquals("a", Strings.sanitizeWhitespace("a\n ")); }
public static String sanitizeWhitespace(String str) { int start = 0; int end = str.length(); while (start < end && CodepointMatcher.ASCII_WHITESPACE.matches(str.charAt(start))) { start++; } if (start == end) { return ""; } while (end > start && CodepointMatcher.ASCII_WHITESPACE.matches(str.charAt(end - 1))) { end--; } int firstNewline = -1; for (int i = start; i < end; i++) { if (CodepointMatcher.ASCII_NEWLINE.matches(str.charAt(i))) { firstNewline = i; break; } } if (firstNewline == -1) { if (start == 0 && end == str.length()) { return str; } else { return str.substring(start, end); } } char[] chars = str.toCharArray(); int p = firstNewline; for (int i = firstNewline; i < end; i++) { if (CodepointMatcher.ASCII_NEWLINE.matches(str.charAt(i))) { i++; while (i < end && CodepointMatcher.ASCII_WHITESPACE.matches(str.charAt(i))) { i++; } } chars[p++] = str.charAt(i); } return new String(chars, start, p - start); }
Strings { public static String sanitizeWhitespace(String str) { int start = 0; int end = str.length(); while (start < end && CodepointMatcher.ASCII_WHITESPACE.matches(str.charAt(start))) { start++; } if (start == end) { return ""; } while (end > start && CodepointMatcher.ASCII_WHITESPACE.matches(str.charAt(end - 1))) { end--; } int firstNewline = -1; for (int i = start; i < end; i++) { if (CodepointMatcher.ASCII_NEWLINE.matches(str.charAt(i))) { firstNewline = i; break; } } if (firstNewline == -1) { if (start == 0 && end == str.length()) { return str; } else { return str.substring(start, end); } } char[] chars = str.toCharArray(); int p = firstNewline; for (int i = firstNewline; i < end; i++) { if (CodepointMatcher.ASCII_NEWLINE.matches(str.charAt(i))) { i++; while (i < end && CodepointMatcher.ASCII_WHITESPACE.matches(str.charAt(i))) { i++; } } chars[p++] = str.charAt(i); } return new String(chars, start, p - start); } }
Strings { public static String sanitizeWhitespace(String str) { int start = 0; int end = str.length(); while (start < end && CodepointMatcher.ASCII_WHITESPACE.matches(str.charAt(start))) { start++; } if (start == end) { return ""; } while (end > start && CodepointMatcher.ASCII_WHITESPACE.matches(str.charAt(end - 1))) { end--; } int firstNewline = -1; for (int i = start; i < end; i++) { if (CodepointMatcher.ASCII_NEWLINE.matches(str.charAt(i))) { firstNewline = i; break; } } if (firstNewline == -1) { if (start == 0 && end == str.length()) { return str; } else { return str.substring(start, end); } } char[] chars = str.toCharArray(); int p = firstNewline; for (int i = firstNewline; i < end; i++) { if (CodepointMatcher.ASCII_NEWLINE.matches(str.charAt(i))) { i++; while (i < end && CodepointMatcher.ASCII_WHITESPACE.matches(str.charAt(i))) { i++; } } chars[p++] = str.charAt(i); } return new String(chars, start, p - start); } private  Strings(); }
Strings { public static String sanitizeWhitespace(String str) { int start = 0; int end = str.length(); while (start < end && CodepointMatcher.ASCII_WHITESPACE.matches(str.charAt(start))) { start++; } if (start == end) { return ""; } while (end > start && CodepointMatcher.ASCII_WHITESPACE.matches(str.charAt(end - 1))) { end--; } int firstNewline = -1; for (int i = start; i < end; i++) { if (CodepointMatcher.ASCII_NEWLINE.matches(str.charAt(i))) { firstNewline = i; break; } } if (firstNewline == -1) { if (start == 0 && end == str.length()) { return str; } else { return str.substring(start, end); } } char[] chars = str.toCharArray(); int p = firstNewline; for (int i = firstNewline; i < end; i++) { if (CodepointMatcher.ASCII_NEWLINE.matches(str.charAt(i))) { i++; while (i < end && CodepointMatcher.ASCII_WHITESPACE.matches(str.charAt(i))) { i++; } } chars[p++] = str.charAt(i); } return new String(chars, start, p - start); } private  Strings(); static int[] codePoints(String s); static boolean isNullOrEmpty(String s); static String nullToEmpty(String value); static String sanitizeWhitespace(String str); }
Strings { public static String sanitizeWhitespace(String str) { int start = 0; int end = str.length(); while (start < end && CodepointMatcher.ASCII_WHITESPACE.matches(str.charAt(start))) { start++; } if (start == end) { return ""; } while (end > start && CodepointMatcher.ASCII_WHITESPACE.matches(str.charAt(end - 1))) { end--; } int firstNewline = -1; for (int i = start; i < end; i++) { if (CodepointMatcher.ASCII_NEWLINE.matches(str.charAt(i))) { firstNewline = i; break; } } if (firstNewline == -1) { if (start == 0 && end == str.length()) { return str; } else { return str.substring(start, end); } } char[] chars = str.toCharArray(); int p = firstNewline; for (int i = firstNewline; i < end; i++) { if (CodepointMatcher.ASCII_NEWLINE.matches(str.charAt(i))) { i++; while (i < end && CodepointMatcher.ASCII_WHITESPACE.matches(str.charAt(i))) { i++; } } chars[p++] = str.charAt(i); } return new String(chars, start, p - start); } private  Strings(); static int[] codePoints(String s); static boolean isNullOrEmpty(String s); static String nullToEmpty(String value); static String sanitizeWhitespace(String str);  }