Example usage for com.google.common.base CharMatcher trimTrailingFrom

List of usage examples for com.google.common.base CharMatcher trimTrailingFrom

Introduction

In this page you can find the example usage for com.google.common.base CharMatcher trimTrailingFrom.

Prototype

@CheckReturnValue
public String trimTrailingFrom(CharSequence sequence) 

Source Link

Document

Returns a substring of the input character sequence that omits all characters this matcher matches from the end of the string.

Usage

From source file:io.v.impl.google.naming.NamingUtil.java

/**
 * Reduces multiple adjacent slashes to a single slash and removes any trailing slash.
 *//*from   w w  w  .  j a v  a2s  .  com*/
public static String clean(String name) {
    CharMatcher slashMatcher = CharMatcher.is('/');
    name = slashMatcher.collapseFrom(name, '/');
    if ("/".equals(name)) {
        return name;
    }
    return slashMatcher.trimTrailingFrom(name);
}

From source file:com.android.mail.lib.base.StringUtil.java

/**
 * If the given string is of length {@code maxLength} or less, then it is
 * returned as is.//  w w w  .  j ava 2 s .  c o  m
 * If the string is longer than {@code maxLength}, the returned string is
 * truncated before the last space character on or before
 * {@code source.charAt(maxLength)}. If the string has no spaces, the
 * returned string is truncated to {@code maxLength}.
 *
 * @param source the string to truncate if necessary
 * @param maxLength
 * @return the original string if its length is less than or equal to
 *     maxLength, otherwise a truncated string as mentioned above
 */
public static String truncateIfNecessary(String source, int maxLength) {
    if (source.length() <= maxLength) {
        return source;
    }
    String str = unicodePreservingSubstring(source, 0, maxLength);

    @SuppressWarnings("deprecation") // we'll make this go away before that does
    CharMatcher whitespaceMatcher = CharMatcher.LEGACY_WHITESPACE;
    String truncated = whitespaceMatcher.trimTrailingFrom(str);

    // We may have had multiple spaces at maxLength, which were stripped away
    if (truncated.length() < maxLength) {
        return truncated;
    }
    // We have a truncated string of length maxLength. If the next char was a
    // space, we truncated at a word boundary, so we can return immediately
    if (Character.isSpaceChar(source.charAt(maxLength))) {
        return truncated;
    }
    // We truncated in the middle of the word. Try to truncate before
    // the last space, if it exists. Otherwise, return the truncated string
    for (int i = truncated.length() - 1; i >= 0; --i) {
        if (Character.isSpaceChar(truncated.charAt(i))) {
            String substr = truncated.substring(0, i);
            return whitespaceMatcher.trimTrailingFrom(substr);
        }
    }
    return truncated;
}

From source file:com.android.mail.lib.base.StringUtil.java

/**
 * This is a both way strip.//from  www .  jav a 2  s  . co m
 *
 * @param str the string to strip
 * @param left strip from left
 * @param right strip from right
 * @param what character(s) to strip
 * @return the stripped string
 * @deprecated ensure the string is not null and use
 *  <ul>
 *    <li> {@code CharMatcher.anyOf(what).trimFrom(str)}
 *        if {@code left == true} and {@code right == true}
 *    <li> {@code CharMatcher.anyOf(what).trimLeadingFrom(str)}
 *        if {@code left == true} and {@code right == false}
 *    <li> {@code CharMatcher.anyOf(what).trimTrailingFrom(str)}
 *        if {@code left == false} and {@code right == true}
 *  </ul>
 */
@Deprecated
public static String megastrip(String str, boolean left, boolean right, String what) {
    if (str == null) {
        return null;
    }

    CharMatcher matcher = CharMatcher.anyOf(what);
    if (left) {
        if (right) {
            return matcher.trimFrom(str);
        }
        return matcher.trimLeadingFrom(str);
    }
    if (right) {
        return matcher.trimTrailingFrom(str);
    }
    return str;
}