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.ispy.util.MoreStringUtils.java

/**
 * This method will take a string of characters that you do not want to
 * appear in your string.  It will remove the unallowed characters and then
 * return to you the string as it would be without those characters.  For
 * instance if you passed ":" as the unallowed characters, and "I a:m ::Cle:an"
 * is the string you passed, you would end up with "I am Clean".  It does 
 * not replace the characters it removes them.
 * /*from   w ww. ja v a2s.co m*/
 * @param unallowableCharString --give me a string of all the chars you don't want in your
 * final string.
 * @param stringToClean  --this is the string that you want cleaned
 * @return  your new string
 */
public static String cleanString(String unallowableCharacters, String stringToClean) {
    if (unallowableCharacters != null && stringToClean != null) {
        char[] filterString = unallowableCharacters.toCharArray();
        filterString = stringToClean.toCharArray();
        stringToClean = "";
        for (int i = 0; i < filterString.length; i++) {
            if (unallowableCharacters.indexOf(filterString[i]) == -1) {
                stringToClean = stringToClean.concat(Character.toString(filterString[i]));
            }
        }
    }
    return stringToClean;
}

From source file:net.pj.games.eulerproject.elements.StringPermutator.java

private static boolean isReverseOrdered(String s) {

    if (s.length() == 1) {
        return true;
    }/*from   w  w w  .j a v  a 2  s  .co  m*/
    char prev = s.charAt(0);
    for (char c : s.toCharArray()) {
        if (c > prev) {
            //log.debug("{} is not reverse ordered", s);
            return false;
        }
        prev = c;
    }
    //log.debug("{} is reverse ordered", s);
    return true;

}

From source file:Main.java

public static String longestCommonString(String str1, String str2) {
    if (str1 == null || str2 == null || str1.length() == 0 || str2.length() == 0)
        return null;

    char[] cc1 = str1.toCharArray();
    char[] cc2 = str2.toCharArray();

    for (int i = 0; i < cc1.length; i++) {
        for (int j = 0; j < cc2.length; j++) {
            check(cc1, cc2, i, j);/*  w w  w .j av  a2s .co  m*/
        }
    }

    StringBuilder sb = new StringBuilder();
    while (gLength > 0) {
        sb.append(cc1[gStart++]);
        gLength--;
    }

    return sb.toString();
}

From source file:Main.java

public static byte[] ConvertStringToHexBytesArray(String StringToConvert) {
    StringToConvert = StringToConvert.toUpperCase();
    StringToConvert = StringToConvert.replaceAll(" ", "");
    char[] CharArray = StringToConvert.toCharArray();
    char[] Char = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    int result = 0;
    byte[] ConvertedString = new byte[StringToConvert.length() / 2];
    int iStringLen = (StringToConvert.length());

    for (int i = 0; i < iStringLen; i++) {
        for (int j = 0; j <= 15; j++) {
            if (CharArray[i] == Char[j]) {
                if (i % 2 == 1) {
                    result = result + j;
                    j = 15;/*from  ww w .ja  va  2s  . c o m*/
                }

                else if (i % 2 == 0) {
                    result = result + j * 16;
                    j = 15;
                }

            }
        }
        if (i % 2 == 1) {
            ConvertedString[i / 2] = (byte) result;
            result = 0;
        }
    }

    return ConvertedString;
}

From source file:fi.ilmoeuro.membertrack.util.Crypto.java

public static String hash(String candidate, String salt) {
    try {//from w  w  w .jav a2s  .c  o m
        SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec ks = new PBEKeySpec(candidate.toCharArray(), salt.getBytes(StandardCharsets.US_ASCII), 1024,
                128);
        SecretKey sk = skf.generateSecret(ks);
        Key k = new SecretKeySpec(sk.getEncoded(), "AES");
        return Hex.encodeHexString(k.getEncoded());
    } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) {
        throw new RuntimeException("Error while hashing", ex);
    }
}

From source file:net.hillsdon.fij.text.Escape.java

/**
 * HTML escaping routine.  This is a bit extreme for element content.
 *
 * @param content// w  w  w. j a  v  a2 s. c  o  m
 *          the unescaped content.
 * @return the escaped output.
 */
public static String html(final String content) {
    if (content == null) {
        return "";
    }
    final char[] chars = content.toCharArray();
    final StringBuilder result = new StringBuilder(2 * chars.length);
    for (char character : chars) {
        switch (character) {
        case '<':
            result.append("&lt;");
            break;
        case '>':
            result.append("&gt;");
            break;
        case '&':
            result.append("&amp;");
            break;
        case '"':
            result.append("&quot;");
            break;
        case '\'':
            result.append("&#039;");
            break;
        case '\\':
            result.append("&#092;");
            break;
        default:
            result.append(character);
            break;
        }
    }
    return result.toString();
}

From source file:Main.java

public static boolean isValidScriptName(String s) {
    // an empty or null string cannot be a valid identifier
    if (s == null || s.length() == 0) {
        return false;
    }/*from w ww.j  a v a2 s.  c o  m*/

    char[] c = s.toCharArray();
    if (!Character.isJavaIdentifierStart(c[0])) {
        return false;
    }

    for (int i = 1; i < c.length; i++) {
        if (!Character.isJavaIdentifierPart(c[i])) {
            return false;
        }
    }

    return true;
}

From source file:com.asakusafw.yaess.tools.GenerateExecutionId.java

private static String normalize(String string) {
    assert string != null;
    StringBuilder buf = new StringBuilder();
    for (char c : string.toCharArray()) {
        if (Character.isJavaIdentifierPart(c)) {
            buf.append(c);/*from w  w  w  . j a  v  a2s  .  co  m*/
        } else {
            buf.append('_');
        }
    }
    return buf.toString();
}

From source file:Main.java

public static List<String> splitSqlScript(String script, char delim) {
    List<String> statements = new ArrayList<String>();
    StringBuilder sb = new StringBuilder();
    boolean inLiteral = false;
    char[] content = script.toCharArray();
    for (int i = 0; i < script.length(); i++) {
        if (content[i] == '"') {
            inLiteral = !inLiteral;/* ww w .  j  a  va2s .  c  o m*/
        }
        if (content[i] == delim && !inLiteral) {
            if (sb.length() > 0) {
                statements.add(sb.toString().trim());
                sb = new StringBuilder();
            }
        } else {
            sb.append(content[i]);
        }
    }
    if (sb.length() > 0) {
        statements.add(sb.toString().trim());
    }
    return statements;
}

From source file:io.cloudslang.content.database.utils.Format.java

/**
 * Chop off '\0' character if present in a String.
 * <p/>//from  ww  w.  j  a va2 s  . c o m
 * This only removes 1 trailing '\0' (null) character.
 * TO DO: rename to match @see replaceInvalidXMLCharacters
 * Only caller: com.opsware.pas.content.commons.util.sql.Format
 *
 * @param s String that may have a null character at the end
 * @return a String without a trailing '\0' character.
 */
public static String processNullTerminatedString(String s) {
    char[] charArray = s.toCharArray();
    // Case 1: Empty null terminated String
    if (charArray.length == 1 && (int) charArray[0] <= 0) {
        return "null";
    }
    // Case 2: Non-empty null terminated String
    // Strip trailing '\0' character if any
    if ((int) charArray[charArray.length - 1] <= 0) {
        return s.substring(0, s.length() - 1);
    }
    return s;
}