/** * Returns first index of a whitespace character, starting from specified index offset. */ public static int indexOfWhitespace(String string, int startindex, int endindex) { for (int i = startindex; i < endindex; i++) { if (CharUtil.isWhitespace(string.charAt(i))) { return i; } } return -1; }
public static int indexOfNonWhitespace(String string, int startindex, int endindex) { for (int i = startindex; i < endindex; i++) { if (!CharUtil.isWhitespace(string.charAt(i))) { return i; } } return -1; }
/** * Returns <code>true</code> if string contains only white spaces. */ public static boolean containsOnlyWhitespaces(CharSequence string) { int size = string.length(); for (int i = 0; i < size; i++) { char c = string.charAt(i); if (!CharUtil.isWhitespace(c)) { return false; } } return true; }
/** * Trim whitespaces from the left. */ public static String trimLeft(String src) { int len = src.length(); int st = 0; while ((st < len) && (CharUtil.isWhitespace(src.charAt(st)))) { st++; } return st > 0 ? src.substring(st) : src; }
/** * Trim whitespaces from the right. */ public static String trimRight(String src) { int len = src.length(); int count = len; while ((len > 0) && (CharUtil.isWhitespace(src.charAt(len - 1)))) { len--; } return (len < count) ? src.substring(0, len) : src; }
public static int indexOfNonWhitespace(final String string, final int startindex, final int endindex) { for (int i = startindex; i < endindex; i++) { if (!CharUtil.isWhitespace(string.charAt(i))) { return i; } } return -1; }
/** * Returns first index of a whitespace character, starting from specified index offset. */ public static int indexOfWhitespace(final String string, final int startindex, final int endindex) { for (int i = startindex; i < endindex; i++) { if (CharUtil.isWhitespace(string.charAt(i))) { return i; } } return -1; }
/** * Trim whitespaces from the left. */ public static String trimLeft(final String src) { int len = src.length(); int st = 0; while ((st < len) && (CharUtil.isWhitespace(src.charAt(st)))) { st++; } return st > 0 ? src.substring(st) : src; }
/** * Returns <code>true</code> if string contains only white spaces. */ public static boolean containsOnlyWhitespaces(final CharSequence string) { int size = string.length(); for (int i = 0; i < size; i++) { char c = string.charAt(i); if (!CharUtil.isWhitespace(c)) { return false; } } return true; }
/** * Trim whitespaces from the right. */ public static String trimRight(final String src) { int len = src.length(); int count = len; while ((len > 0) && (CharUtil.isWhitespace(src.charAt(len - 1)))) { len--; } return (len < count) ? src.substring(0, len) : src; }
/** * Shorten string to given length. */ public static String shorten(String s, int length, String suffix) { length -= suffix.length(); if (s.length() > length) { for (int j = length; j >= 0; j--) { if (CharUtil.isWhitespace(s.charAt(j))) { length = j; break; } } String temp = s.substring(0, length); s = temp.concat(suffix); } return s; }
/** * Appends missing space if the output doesn't end with whitespace. */ protected void appendMissingSpace(final StringBuilder out) { int len = out.length(); if (len == 0) { return; } len--; if (!CharUtil.isWhitespace(out.charAt(len))) { out.append(' '); } }
/** * Makes a title-cased string from given input. */ public static String title(String string) { char[] chars = string.toCharArray(); boolean wasWhitespace = true; for (int i = 0; i < chars.length; i++) { char c = chars[i]; if (CharUtil.isWhitespace(c)) { wasWhitespace = true; } else { if (wasWhitespace) { chars[i] = Character.toUpperCase(c); } else { chars[i] = Character.toLowerCase(c); } wasWhitespace = false; } } return new String(chars); }
/** * Shorten string to given length. */ public static String shorten(String s, int length, final String suffix) { length -= suffix.length(); if (s.length() > length) { for (int j = length; j >= 0; j--) { if (CharUtil.isWhitespace(s.charAt(j))) { length = j; break; } } String temp = s.substring(0, length); s = temp.concat(suffix); } return s; }
/** * Makes a title-cased string from given input. */ public static String title(final String string) { char[] chars = string.toCharArray(); boolean wasWhitespace = true; for (int i = 0; i < chars.length; i++) { char c = chars[i]; if (CharUtil.isWhitespace(c)) { wasWhitespace = true; } else { if (wasWhitespace) { chars[i] = Character.toUpperCase(c); } else { chars[i] = Character.toLowerCase(c); } wasWhitespace = false; } } return new String(chars); }
/** * Extracts encoding from a given content type. * * @param contentType content type. * @return Encoding from the content type. May return {@code null} if encoding is not specified in content type. */ //TODO: should this always return lowercase or always uppercase? public static String extractEncoding(final String contentType) { int ndx = contentType.indexOf(';'); final String charset = ndx != -1 ? contentType.substring(ndx + 1) : StringPool.EMPTY; String encoding = null; ndx = charset.indexOf(ATTR_CHARSET); if (ndx != -1) { ndx += ATTR_CHARSET.length(); final int len = charset.length(); if (charset.charAt(ndx) == '"') { ndx++; } final int start = ndx; while (ndx < len) { final char c = charset.charAt(ndx); if ((c == '"') || (CharUtil.isWhitespace(c)) || (c == ';')) { break; } ndx++; } encoding = charset.substring(start, ndx); } return encoding; }
char c = text.charAt(i); if (CharUtil.isWhitespace(c)) { if (regularChar) { regularChar = false;
@ParameterizedTest (name = "{index} - CharUtil.isWhitespace({1}) == {0}") @MethodSource({"testdata_testIsWhitespace"}) void testIsWhitespace(final boolean expected, final char input) { assertEquals(expected, CharUtil.isWhitespace(input)); }
/** * Returns first index of a whitespace character, starting from specified index offset. */ public static int indexOfWhitespace(final String string, final int startindex, final int endindex) { for (int i = startindex; i < endindex; i++) { if (CharUtil.isWhitespace(string.charAt(i))) { return i; } } return -1; }
/** * Trim whitespaces from the right. */ public static String trimRight(final String src) { int len = src.length(); int count = len; while ((len > 0) && (CharUtil.isWhitespace(src.charAt(len - 1)))) { len--; } return (len < count) ? src.substring(0, len) : src; }