Example usage for java.lang String toCharArray

List of usage examples for java.lang String toCharArray

Introduction

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

Prototype

public char[] toCharArray() 

Source Link

Document

Converts this string to a new character array.

Usage

From source file:gov.nih.nci.caintegrator.application.zip.FileNameGenerator.java

protected static String removeSpaces(String name) {
    StringBuffer buf = new StringBuffer(name.length());
    for (Character c : name.toCharArray()) {
        if (' ' == c || '\'' == c || '\"' == c || '?' == c || ',' == c || '&' == c || '' == c || '' == c
                || '#' == c)
            buf.append('_');
        else//from  w  w w .ja v  a  2  s.c o  m
            buf.append(c);
    }
    return new String(buf);
}

From source file:Main.java

public static String replaceSpecialCharacters(String value) {
    if (value == null)
        return value;
    //only replace & if it not already an escaped sequence
    char[] val = value.toCharArray();
    int pos = 0;//from  www. j  a  v  a2  s  . c o m

    while (pos < val.length) {
        if (val[pos] == '&') {
            int ampPos = pos;
            //move over until last non-whitespace character to see if ;
            while (pos < val.length && (val[pos] + "").matches("[^\\s]") && pos < ampPos + 5) {
                pos++;
            }

            if (val[pos] != ';') {
                pos = pos + 3;
                String newValue = value.substring(0, ampPos) + "&amp;";
                if (val.length > pos + 1) {
                    newValue += value.substring(ampPos + 1);
                }
                value = newValue;
                val = value.toCharArray();
            }
        }
        pos++;
    }
    value = value.replace("<", "&lt;");
    value = value.replace(">", "&gt;");
    value = value.replace("\"", "&quot;");
    value = value.replace("'", "&apos;");
    return value;
}

From source file:Main.java

public static String escapeAttribute(String value) {
    if (value == null)
        return "";

    StringBuffer result = new StringBuffer(value.length());
    char[] chars = value.toCharArray();
    for (int i = 0; i < chars.length; i++) {
        switch (chars[i]) {
        case '<':
            result.append("&lt;");
            break;
        case '>':
            result.append("&gt;");
            break;
        case '&':
            result.append("&amp;");
            break;
        case '"':
            result.append("&quot;");
            break;
        case '\'':
            result.append("&apos;");
            break;
        default://  ww  w  .ja va 2 s.  c o  m
            if (chars[i] < 32)
                result.append("&#").append((int) chars[i]).append(";");
            else
                result.append(chars[i]);
        }
    }
    return result.toString();
}

From source file:com.haulmont.timesheets.EncryptDecrypt.java

private static byte[] decodeString(String string) throws DecoderException {
    if (string == null)
        return null;
    return Hex.decodeHex(string.toCharArray());
}

From source file:com.ai.tools.generator.util.XMLFormatter.java

public static String fixProlog(String xml) {
    // LEP-1921/*from   w  ww.j  a v  a  2s .c  o  m*/
    if (xml != null) {
        char[] charArray = xml.toCharArray();

        for (int i = 0; i < charArray.length; i++) {
            if (charArray[i] == '<') {
                if (i != 0) {
                    xml = xml.substring(i, xml.length());
                }

                break;
            }
        }
    }

    return xml;
}

From source file:czlab.xlib.CU.java

/**
 * Shuffle characters in this string./*from  w  w w.ja v a  2 s  .  co  m*/
 *
 * @param s
 * @return
 */
public static String shuffle(String s) {
    List<Character> lst = new ArrayList<>();
    char[] cs = s.toCharArray();
    for (int n = 0; n < cs.length; ++n) {
        lst.add(cs[n]);
    }
    Collections.shuffle(lst);
    for (int n = 0; n < lst.size(); ++n) {
        cs[n] = lst.get(n).charValue();
    }
    return new String(cs);
}

From source file:models.logic.CipherDecipher.java

public static SecretKey stringToKey(String keyString) {
    byte[] encoded;
    try {//from ww w. ja  va  2s .  com
        encoded = decodeHex(keyString.toCharArray());
    } catch (DecoderException e) {
        e.printStackTrace();
        return null;
    }
    return new SecretKeySpec(encoded, "AES");
}

From source file:Main.java

public static String normalizeName(String strName) {
    if (strName == null)
        return null;
    if (strName.length() == 0)
        return strName;

    char[] strChars = strName.toCharArray();
    char firstChar = strName.charAt(0);
    if (!(((firstChar >= 'a') && (firstChar <= 'z')) || ((firstChar >= 'A') && (firstChar <= 'Z'))
            || (firstChar == '_'))) {
        strChars = strName.toCharArray();
        strChars[0] = '_';
    }/*from  ww  w  .j  av  a 2s. co m*/

    int len = strName.length();
    for (int i = 1; i < len; i++) {
        char c = strName.charAt(i);
        if (!(((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) || ((c >= '0') && (c <= '9'))
                || (c == '_') || (c == '-'))) {
            if (strChars == null)
                strChars = strName.toCharArray();
            strChars[i] = '_';
        }
    }
    if (strChars == null)
        return strName;
    else
        return new String(strChars);
}

From source file:edu.lternet.pasta.common.HTMLUtility.java

/**
 * This method helps to ensure that the output String has only valid HTML
 * characters by stripping out invalid control characters.
 *
 * @param in The String whose non-valid characters we want to remove.
 * @return The input String, stripped of non-valid characters.
 *///from ww w  . ja v  a2  s.  co m
public static String stripNonValidHTMLCharacters(String in) {

    char[] charBuf = null;

    if (in != null && !in.isEmpty()) {

        charBuf = in.toCharArray();
        int charBufSize = charBuf.length;

        for (int i = 0; i < charBufSize; i++) {

            if (isInvalidHTMLCharacter(charBuf[i])) {
                charBuf[i] = '\uFFFD'; // Replace illegal character with replacement character
            }

        }

    }

    return new String(charBuf);

}

From source file:Main.java

private static String cleanupSearchString(String search) {
    if (search == null)
        return "";

    StringBuilder out = new StringBuilder(search.length());
    char prev = ' ';
    for (char curr : search.toCharArray()) {
        if (Character.isLetterOrDigit(curr)) {
            out.append(curr);/*w  ww  . j  av  a 2s .c  om*/
        } else {
            curr = ' ';
            if (!Character.isWhitespace(prev)) {
                out.append(curr);
            }
        }
        prev = curr;
    }
    return out.toString();
}