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

static String replaceProperties(final CharSequence string, final Map<String, Object> properties) {
    if (string == null) {
        return null;
    } else {//from   w w  w . j  ava  2 s  .  c  om
        final StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < string.length(); ++i) {
            char c = string.charAt(i);
            switch (c) {
            case '$':
                ++i;
                if (i < string.length()) {
                    c = string.charAt(i);
                    if (c == '{') {
                        ++i;
                        final StringBuilder propertyName = new StringBuilder();
                        for (; i < string.length() && c != '}'; ++i) {
                            c = string.charAt(i);
                            if (c != '}') {
                                propertyName.append(c);
                            }
                        }
                        Object value = null;
                        if (propertyName.length() > 0) {
                            value = properties.get(propertyName.toString());

                        }
                        if (value == null) {
                            buffer.append("${");
                            buffer.append(propertyName);
                            buffer.append("}");
                        } else {
                            buffer.append(value);
                        }
                    }
                }
                break;

            default:
                buffer.append(c);
                break;
            }
        }
        return buffer.toString();
    }
}

From source file:org.mariotaku.twidere.util.CompareUtils.java

public static boolean textEquals(CharSequence text1, CharSequence text2) {
    if (text1 == null || text2 == null)
        return text1 == text2;
    if (text1 == text2)
        return true;
    if (text1.length() != text2.length())
        return false;
    for (int i = 0, j = text1.length(); i < j; i++) {
        if (text1.charAt(i) != text2.charAt(i))
            return false;
    }//from w ww  . j  a va  2  s. c o m
    return true;
}

From source file:Main.java

/**
 * <p>Finds the first index in the {@code CharSequence} that matches the
 * specified character.</p>/*w  w w .  j a  v a2s  .  c  om*/
 *
 * @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);
    }
    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 NOT_FOUND;
}

From source file:Main.java

/**
 * Replaces successive XML space characters (<tt>'\t'</tt>,
 * <tt>'\r'</tt>, <tt>'\n'</tt>, <tt>' '</tt>) by a single space
 * character (<tt>' '</tt>).
 * //w w  w  . ja va2  s. c  o  m
 * @param value string to be processed
 * @param buffer buffer used to store processed characters (characters are
 *        appended to this buffer)
 */
private static void compressWhiteSpace(CharSequence value, StringBuffer buffer) {
    // No need to convert "\r\n" to a single '\n' because white spaces
    // are compressed.

    int length = value.length();
    char prevChar = '?';
    for (int i = 0; i < length; ++i) {
        char c = value.charAt(i);

        switch (c) {
        case '\t':
        case '\r':
        case '\n':
            c = ' ';
            break;
        }

        if (c == ' ') {
            if (prevChar != ' ') {
                buffer.append(c);
                prevChar = c;
            }
        } else {
            buffer.append(c);
            prevChar = c;
        }
    }
}

From source file:Arrays.java

/**
 * Converts the specified character sequence to an array
 * of characters.//from  w  w  w  . j a  v a2 s.com
 *
 * @param cSeq Character sequence to convert.
 * @return Array of characters in sequence.
 */
public static char[] toArray(CharSequence cSeq) {
    // return cSeq.toString().toCharArray();
    char[] cs = new char[cSeq.length()];
    for (int i = 0; i < cs.length; ++i)
        cs[i] = cSeq.charAt(i);
    return cs;
}

From source file:Main.java

public static boolean equals(CharSequence a, CharSequence b) {
    if (a == b)/*www .  j  a v a2  s  .c o  m*/
        return true;
    int length;
    if (a != null && b != null && (length = a.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 first index in the {@code CharSequence} that matches the
 * specified character.</p>//w  w  w . j a  v  a  2  s.co m
 *
 * @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(CharSequence cs, int searchChar, int start) {
    if (cs instanceof String) {
        return ((String) cs).indexOf(searchChar, start);
    } else {
        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:Main.java

/**
 * Checks whether the supplied String is an NCName (Namespace Classified
 * Name) as specified at <a/*from w  w w .  j a  v  a2s .c  o  m*/
 * href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">
 * http://www.w3.org/TR/REC-xml-names/#NT-NCName</a>.
 */
public static boolean isNCName(CharSequence name) {
    int nameLength = name.length();
    if (nameLength == 0) {
        return false;
    }
    // Check first character
    char c = name.charAt(0);
    if (c == '_' || isLetter(c)) {
        // Check the rest of the characters
        for (int i = 1; i < nameLength; i++) {
            c = name.charAt(i);
            if (!isNCNameChar(c)) {
                return false;
            }
        }
        // All characters have been checked
        return true;
    }
    return false;
}

From source file:com.appglu.impl.util.StringUtils.java

public static boolean isNotEmpty(CharSequence str) {
    if (!hasLength(str)) {
        return false;
    }// ww  w . java  2  s .  c o m
    int strLen = str.length();
    for (int i = 0; i < strLen; i++) {
        if (!Character.isWhitespace(str.charAt(i))) {
            return true;
        }
    }
    return false;
}

From source file:org.apache.hadoop.io.crypto.bee.Hex.java

public static byte[] decode(CharSequence s) {
    int nChars = s.length();

    if (nChars % 2 != 0) {
        throw new IllegalArgumentException("Hex-encoded string must have an even number of characters");
    }// w  w w .ja  v a2  s  .  com

    byte[] result = new byte[nChars / 2];

    for (int i = 0; i < nChars; i += 2) {
        int msb = Character.digit(s.charAt(i), 16);
        int lsb = Character.digit(s.charAt(i + 1), 16);

        if (msb < 0 || lsb < 0) {
            throw new IllegalArgumentException("Non-hex character in input: " + s);
        }
        result[i / 2] = (byte) ((msb << 4) | lsb);
    }
    return result;
}