Example usage for java.lang CharSequence charAt

List of usage examples for java.lang CharSequence charAt

Introduction

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

Prototype

char charAt(int index);

Source Link

Document

Returns the char value at the specified index.

Usage

From source file:Main.java

/**
 * <p>Finds the first index in the {@code CharSequence} that matches the
 * specified character.</p>//  www  . j  a v a  2s . com
 *
 * @param cs  the {@code CharSequence} to be processed, not null
 * @param searchChar  the char to be searched for
 * @param start  the start index, negative starts at the string start
 * @return the index where the search char was found, -1 if not found
 */
static int indexOf(final CharSequence cs, final int searchChar, int start) {
    if (cs instanceof String) {
        return ((String) cs).indexOf(searchChar, start);
    } else {
        final int sz = cs.length();
        if (start < 0) {
            start = 0;
        }
        for (int i = start; i < sz; i++) {
            if (cs.charAt(i) == searchChar) {
                return i;
            }
        }
        return -1;
    }
}

From source file:com.laxser.blitz.util.BlitzStringUtil.java

public static boolean startsWith(CharSequence input, String prefix) {
    if (input.length() < prefix.length()) {
        return false;
    }// w  ww  . j a v  a 2  s. com
    if (input.getClass() == String.class) {
        return ((String) input).startsWith(prefix);
    }
    int len = prefix.length();
    for (int i = 0; i < len; i++) {
        char pi = input.charAt(i);
        char ci = prefix.charAt(i);
        if (pi != ci) {
            return false;
        }
    }
    return true;
}

From source file:org.springframework.core.util.StringUtils.java

/**
 * Check whether the given CharSequence has actual text.
 * More specifically, returns <code>true</code> if the string not <code>null</code>,
 * its length is greater than 0, and it contains at least one non-whitespace character.
 * <p><pre>//from   w  ww  .ja va2 s.  c o m
 * org.springframework.core.util.StringUtils.hasText(null) = false
 * org.springframework.core.util.StringUtils.hasText("") = false
 * org.springframework.core.util.StringUtils.hasText(" ") = false
 * org.springframework.core.util.StringUtils.hasText("12345") = true
 * org.springframework.core.util.StringUtils.hasText(" 12345 ") = true
 * </pre>
 *
 * @param str the CharSequence to check (may be <code>null</code>)
 * @return <code>true</code> if the CharSequence is not <code>null</code>,
 *         its length is greater than 0, and it does not contain whitespace only
 * @see Character#isWhitespace
 */
public static boolean hasText(final CharSequence str) {
    if (!hasLength(str)) {
        return false;
    }
    int strLen = str.length();
    for (int i = 0; i < strLen; i++) {
        if (!Character.isWhitespace(str.charAt(i))) {
            return true;
        }
    }
    return false;
}

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

/**
 * Encodes characters in the query or fragment part of the URI.
 *
 * <p>//from   ww w.j  ava2s . 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;
}

From source file:se.sawano.java.security.otp.google.keyuri.UnicodeEscaper.java

/**
 * Returns the Unicode code point of the character at the given index.
 *
 * <p>Unlike {@link Character#codePointAt(CharSequence, int)} or {@link String#codePointAt(int)}
 * this method will never fail silently when encountering an invalid surrogate pair.
 *
 * <p>The behaviour of this method is as follows:
 * <ol>//  ww w.jav  a 2 s.co  m
 * <li>If {@code index >= end}, {@link IndexOutOfBoundsException} is thrown.
 * <li><b>If the character at the specified index is not a surrogate, it is returned.</b>
 * <li>If the first character was a high surrogate value, then an attempt is made to read the next
 * character.
 * <ol>
 * <li><b>If the end of the sequence was reached, the negated value of the trailing high
 * surrogate is returned.</b>
 * <li><b>If the next character was a valid low surrogate, the code point value of the
 * high/low surrogate pair is returned.</b>
 * <li>If the next character was not a low surrogate value, then {@link
 * IllegalArgumentException} is thrown.
 * </ol>
 * <li>If the first character was a low surrogate value, {@link IllegalArgumentException} is
 * thrown.
 * </ol>
 *
 * @param seq
 *         the sequence of characters from which to decode the code point
 * @param index
 *         the index of the first character to decode
 * @param end
 *         the index beyond the last valid character to decode
 *
 * @return the Unicode code point for the given index or the negated value of the trailing high surrogate character at the end of the sequence
 */
protected static int codePointAt(CharSequence seq, int index, int end) {
    notNull(seq);
    if (index < end) {
        char c1 = seq.charAt(index++);
        if (c1 < Character.MIN_HIGH_SURROGATE || c1 > Character.MAX_LOW_SURROGATE) {
            // Fast path (first test is probably all we need to do)
            return c1;
        } else if (c1 <= Character.MAX_HIGH_SURROGATE) {
            // If the high surrogate was the last character, return its inverse
            if (index == end) {
                return -c1;
            }
            // Otherwise look for the low surrogate following it
            char c2 = seq.charAt(index);
            if (Character.isLowSurrogate(c2)) {
                return Character.toCodePoint(c1, c2);
            }
            throw new IllegalArgumentException("Expected low surrogate but got char '" + c2 + "' with value "
                    + (int) c2 + " at index " + index + " in '" + seq + "'");
        } else {
            throw new IllegalArgumentException("Unexpected low surrogate character '" + c1 + "' with value "
                    + (int) c1 + " at index " + (index - 1) + " in '" + seq + "'");
        }
    }
    throw new IndexOutOfBoundsException("Index exceeds specified range");
}

From source file:org.rm3l.ddwrt.tiles.status.wireless.WirelessIfaceQrCodeActivity.java

@Nullable
private static String guessAppropriateEncoding(@NotNull final CharSequence contents) {
    // Very crude at the moment
    for (int i = 0; i < contents.length(); i++) {
        if (contents.charAt(i) > 0xFF) {
            return "UTF-8";
        }//from  www .  j av a2s . c  o m
    }
    return null;
}

From source file:Main.java

/**
 * Returns true if a and b are equal, including if they are both null.
 * <p><i>Note: In platform versions 1.1 and earlier, this method only worked well if
 * both the arguments were instances of String.</i></p>
 *
 * @param a first CharSequence to check/*from   w  w w . ja  va  2  s . c  o m*/
 * @param b second CharSequence to check
 *
 * @return true if a and b are equal
 *
 * NOTE: Logic slightly change due to strict policy on CI -
 * "Inner assignments should be avoided"
 */
static boolean equals(CharSequence a, CharSequence b) {
    if (a == b)
        return true;
    if (a != null && b != null) {
        int length = a.length();
        if (length == b.length()) {
            if (a instanceof String && b instanceof String) {
                return a.equals(b);
            } else {
                for (int i = 0; i < length; i++) {
                    if (a.charAt(i) != b.charAt(i))
                        return false;
                }
                return true;
            }
        }
    }
    return false;
}

From source file:Main.java

/**
 * <p>Finds the last index in the {@code CharSequence} that matches the
 * specified character.</p>//w ww  .j a v  a  2 s  .c  o m
 *
 * @param cs         the {@code CharSequence} to be processed
 * @param searchChar the char to be searched for
 * @param start      the start index, negative returns -1, beyond length starts at end
 * @return the index where the search char was found, -1 if not found
 */
static int lastIndexOf(final CharSequence cs, final int searchChar, int start) {
    if (cs instanceof String) {
        return ((String) cs).lastIndexOf(searchChar, start);
    }
    final int sz = cs.length();
    if (start < 0) {
        return NOT_FOUND;
    }
    if (start >= sz) {
        start = sz - 1;
    }
    for (int i = start; i >= 0; --i) {
        if (cs.charAt(i) == searchChar) {
            return i;
        }
    }
    return NOT_FOUND;
}

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

/**
 * @param   text      Text to be analyzed
 * @param   offset      Character offset in text, intersecting the word dealing with
 * @return  int         Starting position offset of word at given offset in given CharSequence
 *///  w  w  w.j a va 2  s .c  o  m
public static int getStartOfWordAtOffset(CharSequence text, int offset) {
    if (text.length() == 0)
        return 0;

    if (offset > 0 && !Character.isJavaIdentifierPart(text.charAt(offset))
            && Character.isJavaIdentifierPart(text.charAt(offset - 1))) {
        offset--;
    }

    if (Character.isJavaIdentifierPart(text.charAt(offset))) {
        int start = offset;

        while (start > 0 && Character.isJavaIdentifierPart(text.charAt(start - 1))) {
            start--;
        }

        return start;
    }

    return 0;
}

From source file:Main.java

/**
 * <p>Finds the last index in the {@code CharSequence} that matches the
 * specified character.</p>//from www . ja va2 s  .  c  o  m
 *
 * @param cs  the {@code CharSequence} to be processed
 * @param searchChar  the char to be searched for
 * @param start  the start index, negative returns -1, beyond length starts at end
 * @return the index where the search char was found, -1 if not found
 */
static int lastIndexOf(CharSequence cs, int searchChar, int start) {
    if (cs instanceof String) {
        return ((String) cs).lastIndexOf(searchChar, start);
    } else {
        int sz = cs.length();
        if (start < 0) {
            return -1;
        }
        if (start >= sz) {
            start = sz - 1;
        }
        for (int i = start; i >= 0; --i) {
            if (cs.charAt(i) == searchChar) {
                return i;
            }
        }
        return -1;
    }
}