Example usage for java.lang String getChars

List of usage examples for java.lang String getChars

Introduction

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

Prototype

public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) 

Source Link

Document

Copies characters from this string into the destination character array.

Usage

From source file:Main.java

/**
 * Convert a String to a char array/*from w  ww  . j a va 2 s  . com*/
 *
 * @param str The string to convert
 * @return character array containing the characters in str. An empty array is returned if str is null.
 */
public static char[] strToChars(String str) {
    int len;
    char[] ret;

    if (str == null) {
        return EMPTY_CHAR_ARRAY;
    }
    len = str.length();
    ret = new char[len];
    str.getChars(0, len, ret, 0);
    return ret;
}

From source file:Main.java

public static int hashCode(String str) {
    int hash = 0;
    if (str != null) {
        int multiplier = 1;
        int offset = 0;
        int count = str.length();
        char[] value = new char[count];
        str.getChars(offset, count, value, 0);
        for (int i = offset + count - 1; i >= offset; i--) {
            hash += value[i] * multiplier;
            int shifted = multiplier << 5;
            multiplier = shifted - multiplier;
        }/*  www.  j  ava  2s  . c  om*/
    }
    return hash;
}

From source file:Main.java

/** Optimized form of: key + "=" + val */
static String mapEntryToString(Object key, Object val) {
    final String k, v;
    final int klen, vlen;
    final char[] chars = new char[(klen = (k = objectToString(key)).length())
            + (vlen = (v = objectToString(val)).length()) + 1];
    k.getChars(0, klen, chars, 0);
    chars[klen] = '=';
    v.getChars(0, vlen, chars, klen + 1);
    return new String(chars);
}

From source file:com.sap.prd.mobile.ios.mios.XCodeValidationCheckMojoTest.java

private static String firstCharToUpperCase(String str) {
    char[] c = new char[str.length()];
    str.getChars(0, str.length(), c, 0);
    c[0] = Character.toUpperCase(c[0]);
    return new String(c);
}

From source file:Main.java

/**
 * Translates special characters to xml equivalents
 * like <b>&gt;</b> and <b>&amp;</b>. Please note that non valid xml chars
 * are not changed, hence you might want to use
 * replaceNonValidXmlCharacters() or stripNonValidXmlCharacters() too.
 *//*  w  ww  .ja v a2  s. c  o  m*/
public static String encodeChars(String string) {
    if (string == null) {
        return null;
    }
    int length = string.length();
    char[] characters = new char[length];
    string.getChars(0, length, characters, 0);
    return encodeChars(characters, 0, length);
}

From source file:dev.dposadsky.java.swingteacherdesktop.utils.StringUtils.java

public static String removeSpace(String s) {
    if (s == null)
        return null;
    char[] buf = new char[1024];
    int length = s.length();
    char[] oldChars = (length < 1024) ? buf : new char[length];
    s.getChars(0, length, oldChars, 0);
    int newLen = 0;
    for (int j = 0; j < length; j++) {
        char ch = oldChars[j];
        if (ch > ' ') {
            oldChars[newLen] = ch;//from  w w w .j a v a2s  . c  o  m
            newLen++;
        }
    }
    if (newLen != length)
        s = new String(oldChars, 0, newLen);

    return s;

}

From source file:Main.java

/**
 * Converts the given string to a char-array of exactly the given length.
 * If the given string is short than the wanted length, then the array is
 * padded with trailing padding chars. If the string is longer, the last
 * character are cut off that the string has the wanted size.
 *
 * @param string The string to convert./*from   w  ww  .j  ava2  s  . c o m*/
 * @param exactArrayLength The length of the retirned char-array.
 * @param paddingChar The character to use for padding, if necessary.
 * @return The string as char array, padded or cut off, if necessary.
 *         The array will have length exactArrayLength. null, if the
 *         given string is null.
 * @preconditions (exactArrayLength >= 0)
 * @postconditions (result == null)
 *                 or (result <> null)
 *                    and (result.length == exactArrayLength)
 */
public static char[] toPaddedCharArray(String string, int exactArrayLength, char paddingChar) {
    char[] charArray = null;

    if (string != null) {
        int stringLength = string.length();
        charArray = new char[exactArrayLength];
        string.getChars(0, Math.min(stringLength, exactArrayLength), charArray, 0);
        for (int i = stringLength; i < charArray.length; i++) { // fill the rest of the array with padding char
            charArray[i] = paddingChar;
        }
    }

    return charArray;
}

From source file:Main.java

@VisibleForTesting
public static void copyToCharArrayBuffer(String text, CharArrayBuffer buffer) {
    if (text != null) {
        char[] data = buffer.data;
        if (data == null || data.length < text.length()) {
            buffer.data = text.toCharArray();
        } else {/* ww  w  . j  ava 2  s .co  m*/
            text.getChars(0, text.length(), data, 0);
        }
        buffer.sizeCopied = text.length();
    } else {
        buffer.sizeCopied = 0;
    }
}

From source file:Main.java

public static String stripCharFromString(String fullString, char stripChar) {
    String retval = null;//from   ww  w. j a  v  a  2s .c om
    if (null != fullString) {
        int len = fullString.length();
        // first convert string into a char array
        char[] chars = new char[len];

        try {
            fullString.getChars(0, len, chars, 0);
        } catch (Exception e) {

        }

        int i;
        int j = 0;
        for (i = 0; i < chars.length; i++) {
            if (stripChar != chars[i]) {
                chars[j] = chars[i];
                j++;
            }
        }
        retval = new String(chars, 0, j);
    }
    return retval;
}

From source file:ypcnv.helpers.VCardSamsungHelper.java

/**
 * Some fields are encoded unproperly. Workaround - transliterate.
 * //  w  ww  .  j  a  v  a  2s.c  om
 * @param input
 *            - string to be processed.
 * @return Transliterated string.
 */
public static String transliterateCyr2Lat(String input) {
    char[] buff = { ' ' };
    StringBuilder outputBuilder = new StringBuilder();

    for (Integer idx = 0; idx < input.length(); idx++) {
        input.getChars(idx, idx + 1, buff, 0);
        if (VCardSamsungHelperMetaData.TRANSLIT_TABLE.get(buff[0]) == null) {
            outputBuilder.append(buff[0]);
        } else {
            outputBuilder.append(VCardSamsungHelperMetaData.TRANSLIT_TABLE.get(buff[0]));
        }
    }
    return outputBuilder.toString();
}