Example usage for java.lang String codePointBefore

List of usage examples for java.lang String codePointBefore

Introduction

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

Prototype

public int codePointBefore(int index) 

Source Link

Document

Returns the character (Unicode code point) before 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 before index 1 i.e j
    int retval = str.codePointBefore(1);

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

From source file:com.android.quicksearchbox.ShortcutRepositoryImplLog.java

/**
 * Given a string x, this method returns the least string y such that x is not a prefix of y.
 * This is useful to implement prefix filtering by comparison, since the only strings z that
 * have x as a prefix are such that z is greater than or equal to x and z is less than y.
 *
 * @param str A non-empty string. The contract above is not honored for an empty input string,
 *        since all strings have the empty string as a prefix.
 *//*  w w w . j a  va  2 s. c o m*/
private static String nextString(String str) {
    int len = str.length();
    if (len == 0) {
        return str;
    }
    // The last code point in the string. Within the Basic Multilingual Plane,
    // this is the same as str.charAt(len-1)
    int codePoint = str.codePointBefore(len);
    // This should be safe from overflow, since the largest code point
    // representable in UTF-16 is U+10FFFF.
    int nextCodePoint = codePoint + 1;
    // The index of the start of the last code point.
    // Character.charCount(codePoint) is always 1 (in the BMP) or 2
    int lastIndex = len - Character.charCount(codePoint);
    return new StringBuilder(len).append(str, 0, lastIndex) // append everything but the last code point
            .appendCodePoint(nextCodePoint) // instead of the last code point, use successor
            .toString();
}

From source file:com.keylesspalace.tusky.activity.ComposeActivity.java

private static FindCharsResult findStart(String string, int fromIndex, char[] chars) {
    final int length = string.length();
    while (fromIndex < length) {
        FindCharsResult found = findChars(string, fromIndex, chars);
        int i = found.stringIndex;
        if (i < 0) {
            break;
        } else if (i == 0 || i >= 1 && Character.isWhitespace(string.codePointBefore(i))) {
            return found;
        } else {//from  ww w .ja  va 2  s.c o m
            fromIndex = i + 1;
        }
    }
    return new FindCharsResult();
}

From source file:org.eclipse.rdf4j.rio.turtle.TurtleParser.java

/**
 * Pushes back the supplied string by copying it to the front of the buffer.
 * After this method returns, successive calls to {@link #readCodePoint()}
 * will return the code points in the supplied string again, starting at the
 * first in the String../*from ww  w  .ja va 2  s  .c om*/
 *
 * @param string
 *            the string to un-read.
 * @throws IOException
 */
protected void unread(String string) throws IOException {
    for (int i = string.codePointCount(0, string.length()); i >= 1; i--) {
        final int codePoint = string.codePointBefore(i);
        if (Character.isSupplementaryCodePoint(codePoint)) {
            final char[] surrogatePair = Character.toChars(codePoint);
            reader.unread(surrogatePair);
        } else {
            reader.unread(codePoint);
        }
    }
}