Example usage for java.lang CharSequence length

List of usage examples for java.lang CharSequence length

Introduction

In this page you can find the example usage for java.lang CharSequence length.

Prototype

int length();

Source Link

Document

Returns the length of this character sequence.

Usage

From source file:mobile.tiis.appv2.helpers.Utils.java

public static boolean isStringBlank(CharSequence str) {
    int strLen;/*from w w w .  j  a  va  2  s  . c o  m*/
    if (str == null || (strLen = str.length()) == 0) {
        return true;
    }
    for (int i = 0; i < strLen; i++) {
        if ((Character.isWhitespace(str.charAt(i)) == false)) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * Escapes a character sequence so that it is valid XML.
 * //from w  ww. j a  v  a2s .co  m
 * @param s
 *        The character sequence.
 * @return The escaped version of the character sequence.
 */
public static String escapeXML(CharSequence s) {
    // double quote -- quot
    // ampersand -- amp
    // less than -- lt
    // greater than -- gt
    // apostrophe -- apos
    StringBuilder sb = new StringBuilder(s.length() * 2);
    for (int i = 0; i < s.length();) {
        int codePoint = Character.codePointAt(s, i);
        if (codePoint == '<') {
            sb.append(LT);
        } else if (codePoint == '>') {
            sb.append(GT);
        } else if (codePoint == '\"') {
            sb.append(QUOT);
        } else if (codePoint == '&') {
            sb.append(AMP);
        } else if (codePoint == '\'') {
            sb.append(APOS);
        } else {
            sb.appendCodePoint(codePoint);
        }
        i += Character.charCount(codePoint);
    }
    return sb.toString();
}

From source file:Main.java

private static int getTextWidth(final CharSequence text, final TextPaint paint) {
    if (TextUtils.isEmpty(text)) {
        return 0;
    }/*from   ww w .j a v a  2  s  .c o m*/
    final Typeface savedTypeface = paint.getTypeface();
    paint.setTypeface(getTextTypeface(text));
    final int len = text.length();
    final float[] widths = new float[len];
    final int count = paint.getTextWidths(text, 0, len, widths);
    int width = 0;
    for (int i = 0; i < count; i++) {
        width += Math.round(widths[i] + 0.5f);
    }
    paint.setTypeface(savedTypeface);
    return width;
}

From source file:net.myrrix.common.math.MatrixUtils.java

private static void appendWithPadOrTruncate(CharSequence value, StringBuilder to) {
    int length = value.length();
    if (length >= PRINT_COLUMN_WIDTH) {
        to.append(value, 0, PRINT_COLUMN_WIDTH);
    } else {/*from w  w  w . ja  v  a  2s  .  c  o  m*/
        for (int i = length; i < PRINT_COLUMN_WIDTH; i++) {
            to.append(' ');
        }
        to.append(value);
    }
}

From source file:hrytsenko.csv.IO.java

static CsvSchema.Builder getSchema(Map<String, ?> args) {
    CharSequence separator = (CharSequence) args.get("separator");
    if (separator == null) {
        separator = ",";
    }//from   w w  w.j  ava2s . com
    if (separator.length() != 1) {
        throw new IllegalArgumentException("Use single character as separator.");
    }

    CharSequence qualifier = (CharSequence) args.get("qualifier");
    if (qualifier == null) {
        qualifier = "\"";
    }
    if (qualifier.length() != 1) {
        throw new IllegalArgumentException("Use single character as qualifier.");
    }

    CsvSchema.Builder schema = CsvSchema.builder();
    schema.setColumnSeparator(separator.charAt(0));
    schema.setQuoteChar(qualifier.charAt(0));
    return schema;
}

From source file:com.kstenschke.shifter.utils.UtilsTextual.java

/**
 * @param   text        Full text//from ww  w.j  av a 2  s  . c  om
 * @param   offset      Offset from before which to extract one character
 * @return  String      Character BEFORE word at given caret offset
 */
public static String getCharBeforeOffset(CharSequence text, int offset) {
    if (text.length() == 0 || offset == 0)
        return "";

    if (offset > 0) {
        return text.subSequence(offset - 1, offset).toString();
    }

    return "";
}

From source file:com.kstenschke.shifter.utils.UtilsTextual.java

/**
 * @param   text        Full text/*from  ww  w  .ja v  a 2s. co  m*/
 * @param   offset      Offset from after which to extract one character
 * @return  String      Character AFTER word at caret offset
 */
public static String getCharAfterOffset(CharSequence text, int offset) {
    if (text.length() < offset + 2 || offset == 0)
        return "";

    if (offset > 0) {
        return text.subSequence(offset + 1, offset + 2).toString();
    }

    return "";
}

From source file:Main.java

private static void append(StringBuilder sb, CharSequence... data) {
    for (CharSequence d : data) {
        if (d != null) {
            if (d instanceof Spanned) {
                String s = Html.toHtml((Spanned) d);
                sb.append(hexStringFromInt(FLAG_SPANNED, NUM_LEN));
                append(sb, s);/*w  w  w.  j  av a 2s . c om*/
            } else {
                int len = d.length();
                sb.append(hexStringFromInt(len, NUM_LEN)).append(d);
            }
        } else {
            sb.append(hexStringFromInt(FLAG_NULL, NUM_LEN));
        }
    }
}

From source file:Main.java

public static CharSequence addImageToText(Context context, CharSequence text, int resID, int start, int end) {
    SpannableStringBuilder builder = new SpannableStringBuilder(text);

    Drawable d = context.getResources().getDrawable(resID);
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
    builder.setSpan(span, text.length(), 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

    return builder;
}

From source file:io.mapzone.controller.vm.http.HttpForwarder.java

/**
 * Encodes characters in the query or fragment part of the URI.
 *
 * <p>/*from  w  ww  . ja  va  2s.  c om*/
 * Unfortunately, an incoming URI sometimes has characters disallowed by the
 * spec. HttpClient insists that the outgoing proxied request has a valid URI
 * because it uses Java's {@link URI}. To be more forgiving, we must escape the
 * problematic characters. See the URI class for the spec.
 *
 * @param in example: name=value&foo=bar#fragment
 */
protected static CharSequence encodeUriQuery(CharSequence in) {
    // Note that I can't simply use URI.java to encode because it will escape
    // pre-existing escaped things.
    StringBuilder outBuf = null;
    Formatter formatter = null;
    for (int i = 0; i < in.length(); i++) {
        char c = in.charAt(i);
        boolean escape = true;
        if (c < 128) {
            if (asciiQueryChars.get((int) c)) {
                escape = false;
            }
        } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {// not-ascii
            escape = false;
        }
        if (!escape) {
            if (outBuf != null)
                outBuf.append(c);
        } else {
            // escape
            if (outBuf == null) {
                outBuf = new StringBuilder(in.length() + 5 * 3);
                outBuf.append(in, 0, i);
                formatter = new Formatter(outBuf);
            }
            // leading %, 0 padded, width 2, capital hex
            formatter.format("%%%02X", (int) c);// TODO
        }
    }
    return outBuf != null ? outBuf : in;
}