Example usage for java.lang String codePointAt

List of usage examples for java.lang String codePointAt

Introduction

In this page you can find the example usage for java.lang String codePointAt.

Prototype

public int codePointAt(int index) 

Source Link

Document

Returns the character (Unicode code point) at the specified index.

Usage

From source file:Main.java

public static void main(String[] args) {

    String str = "java2s.com";
    System.out.println("String = " + str);

    // codepoint at index 1
    int retval = str.codePointAt(1);

    // prints character at index1 in string
    System.out.println("Character(unicode point) = " + retval);
}

From source file:cz.lidinsky.editor.Action.java

protected static void setLabel(final Action action, final String label) {
    if (label != null) {
        int mnemonicIndex = label.indexOf('_');
        if (mnemonicIndex >= 0) {
            String text = StringUtils.remove(label, '_');
            int key = text.codePointAt(mnemonicIndex);
            action.putValue(Action.NAME, text);
            action.putValue(Action.MNEMONIC_KEY, key);
        } else {//from w w  w.  j  av  a2  s.  co  m
            action.putValue(Action.NAME, label);
        }
    }
}

From source file:info.novatec.testit.livingdoc.util.NameUtils.java

public static boolean isJavaIdentifier(String s) {
    if (!Character.isJavaIdentifierStart(s.codePointAt(0))) {
        return false;
    }//  w ww.  j  a  v a2 s.  c om

    for (int i = 1; i < s.length(); i++) {
        if (!Character.isJavaIdentifierPart(s.codePointAt(i))) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * Anything other than letter and numbers are considered delimiters.  Remove start and end
 * delimiters since they are not relevant to search.
 *
 * @param query The query string to clean.
 * @return The cleaned query. Empty string if all characters are cleaned out.
 *//*  w  w  w.ja va 2s.  c  om*/
public static String cleanStartAndEndOfSearchQuery(String query) {
    int start = 0;
    while (start < query.length()) {
        int codePoint = query.codePointAt(start);
        if (Character.isLetterOrDigit(codePoint)) {
            break;
        }
        start += Character.charCount(codePoint);
    }

    if (start == query.length()) {
        // All characters are delimiters.
        return "";
    }

    int end = query.length() - 1;
    while (end > -1) {
        if (Character.isLowSurrogate(query.charAt(end))) {
            // Assume valid i18n string.  There should be a matching high surrogate before it.
            end--;
        }
        int codePoint = query.codePointAt(end);
        if (Character.isLetterOrDigit(codePoint)) {
            break;
        }
        end--;
    }

    // end is a letter or digit.
    return query.substring(start, end + 1);
}

From source file:Main.java

/**
 * Name ::= NameStartChar (NameChar)*/*w  w w . j  a va 2s.c  om*/
 */
public static boolean isValidXmlName(final String name) {
    if (name == null) {
        return true;
    }
    if (!isValidXmlNameStartChar(name.codePointAt(0))) {
        return false;
    }
    for (int i = 1; i < name.length(); i++) {
        if (!isValidXmlNameChar(name.codePointAt(i))) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * Check CJK codepoints in <code>str</code>.
 * /*from  w w  w .java  2s.c o  m*/
 * @param str
 * @return <code>true</code> if <code>str</code> has at least one CJK codepoint, otherwise
 *         <code>false</code>.
 */
public static boolean isCJK(String str) {
    if (str == null || str.trim().equals(""))
        return false;
    for (int i = 0; i < str.length(); i++) {
        int codePoint = str.codePointAt(i);
        if (codePoint >= 19968 && codePoint <= 40911)
            return true;
    }
    return false;
}

From source file:Main.java

public static boolean isValidHtmlName(final String name) {
    if (name == null) {
        return true;
    }/*from  w w  w  . j a v a2s.  c o m*/
    if (!isValidHtmlNameStartChar(name.codePointAt(0))) {
        return false;
    }
    for (int i = 1; i < name.length(); i++) {
        if (!isValidHtmlNameChar(name.codePointAt(i))) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

public static String parseTelephoneNumber(String sel) {
    if (sel == null || sel.length() == 0)
        return null;

    // Hack: Remove trailing left-to-right mark (Google Maps adds this)
    if (sel.codePointAt(sel.length() - 1) == 8206) {
        sel = sel.substring(0, sel.length() - 1);
    }//  w w w  .j  a va2  s. co  m

    String number = null;
    if (sel.matches("([Tt]el[:]?)?\\s?[+]?(\\(?[0-9|\\s|\\-|\\.]\\)?)+")) {
        String elements[] = sel.split("([Tt]el[:]?)");
        number = elements.length > 1 ? elements[1] : elements[0];
        number = number.replace(" ", "");

        // Remove option (0) in international numbers, e.g. +44 (0)20 ...
        if (number.matches("\\+[0-9]{2,3}\\(0\\).*")) {
            int openBracket = number.indexOf('(');
            int closeBracket = number.indexOf(')');
            number = number.substring(0, openBracket) + number.substring(closeBracket + 1);
        }
    }
    return number;
}

From source file:Main.java

/**
 * Converts a stream of plaintext into valid XML. Output stream must convert
 * stream to UTF-8 when saving to disk./*w w  w.  j ava2  s .co m*/
 */
public static String makeValidXML(String plaintext) {
    StringBuilder out = new StringBuilder();
    String text = removeXMLInvalidChars(plaintext);
    for (int cp, i = 0; i < text.length(); i += Character.charCount(cp)) {
        cp = text.codePointAt(i);
        out.append(escapeXMLChars(cp));
    }
    return out.toString();
}

From source file:Main.java

public static String removeXMLInvalidChars(String str) {
    StringBuilder sb = new StringBuilder(str.length());
    for (int c, i = 0; i < str.length(); i += Character.charCount(c)) {
        c = str.codePointAt(i);
        if (!isValidXMLChar(c)) {
            c = ' ';
        }/*  w  ww .j  a  va 2  s .  c om*/
        sb.appendCodePoint(c);
    }
    return sb.toString();
}