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:io.github.ramizdemiurge.metodlar.java

public static String Do_char(String anyarg) {
    String temp = "";
    char z[] = anyarg.toCharArray();

    for (int i = 0; i <= (z.length - 1); i++) {
        int ascii = (int) z[i];
        if (i < (z.length - 1)) {

            temp += String.valueOf(ascii + ",");
        } else {/*  ww w.j  a v  a  2 s. co m*/
            temp += String.valueOf(ascii);
        }

    }

    return "Char(" + temp + ")";
}

From source file:Main.java

public static String addParentheses(String text) {
    int numOpenParens = 0, numCloseParens = 0;

    //search string for parentheses
    for (char c : text.toCharArray()) {
        if (c == '(') {
            numOpenParens++;//from  w  w  w. java 2  s  .c  o m
        } else if (c == ')') {
            numCloseParens++;
        }
    }

    int diffParens = numOpenParens - numCloseParens;

    //construct a number of open/close parentheses depending on number
    String textCorrected = "";
    if (diffParens == 0) {
        textCorrected = text;
    } else if (diffParens < 0) {
        //add open parentheses at the beginning
        String parens = repeatingChars('(', -diffParens);
        textCorrected = parens + text;
    } else {
        //add close parentheses at the end
        String parens = repeatingChars(')', diffParens);
        textCorrected = text + parens;
    }

    return textCorrected;
}

From source file:Main.java

/**
 * Unescapes a String, replacing &#nn;, &lt;, &gt;, &amp;, &quot;,
 * and &apos to the corresponding characters.
 * @param s   a String with entities/*from www . ja  v a 2  s .  c  om*/
 * @return the unescaped string
 */
public static String unescapeXML(final String s) {
    char[] cc = s.toCharArray();
    int len = cc.length;
    StringBuffer sb = new StringBuffer();
    int pos;
    String esc;
    for (int i = 0; i < len; i++) {
        int c = cc[i];
        if (c == '&') {
            pos = findInArray(';', cc, i + 3);
            if (pos > -1) {
                esc = new String(cc, i + 1, pos - i - 1);
                if (esc.startsWith("#")) {
                    esc = esc.substring(1);
                    if (isValidCharacterValue(esc)) {
                        c = (char) Integer.parseInt(esc);
                        i = pos;
                    } else {
                        i = pos;
                        continue;
                    }
                } else {
                    int tmp = unescape(esc);
                    if (tmp > 0) {
                        c = tmp;
                        i = pos;
                    }
                }
            }
        }
        sb.append((char) c);
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Check if the file provide is PKCS12//from  w w w. ja va2  s  .com
 * @param cert certificate to be validated
 * @param pass password to be provided
 * @throws Exception to indicate an invalid certificate
 */
public static void validate(byte[] cert, String pass) throws Exception {

    try {
        KeyStore keyStore = KeyStore.getInstance(ALGORITHM);
        keyStore.load(new ByteArrayInputStream(cert), pass.toCharArray());
    } catch (Exception e) {
        throw new Exception("Certificate is not valid!", e);
    }
}

From source file:com.glaf.core.security.DefaultEncryptor.java

public static final byte[] decodeHex(String hex) {
    char[] chars = hex.toCharArray();
    byte[] bytes = new byte[chars.length / 2];
    int byteCount = 0;
    for (int i = 0; i < chars.length; i += 2) {
        int newByte = 0x00;
        newByte |= hexCharToByte(chars[i]);
        newByte <<= 4;/*  w ww .java 2  s.c o m*/
        newByte |= hexCharToByte(chars[i + 1]);
        bytes[byteCount] = (byte) newByte;
        byteCount++;
    }
    return bytes;
}

From source file:com.yahoo.bard.webservice.config.ModuleLoader.java

/**
 * A method used to apply validation rules to module names found in resource property files.
 * Throws exceptions when invalid./*from   ww  w  .ja v  a 2s.  c om*/
 *
 * @param name  The name under test.
 *
 * @throws SystemConfigException when a name fails a validation rule
 */
public static void validateModuleName(String name) throws SystemConfigException {
    char[] nameChars = name.toCharArray();

    // Module name should not be a single character
    if (nameChars.length < 2) {
        LOG.error(INVALID_MODULE_CONFIGURATION.logFormat(MODULE_NAME_IS_TOO_SHORT, name));
        throw new SystemConfigException(INVALID_MODULE_CONFIGURATION.format(MODULE_NAME_IS_TOO_SHORT, name));
    }

    List<Character> invalidCharacters = new ArrayList<>(name.length());

    if (!Character.isJavaIdentifierStart(name.charAt(0))) {
        invalidCharacters.add(name.charAt(0));
    }

    name.substring(1).chars().mapToObj(charCode -> (char) charCode)
            .filter(character -> !Character.isJavaIdentifierPart(character) && character != '-')
            .forEach(invalidCharacters::add);

    if (!invalidCharacters.isEmpty()) {
        String message = String.format(ILLEGAL_CHARACTER_IN_MODULE_NAME, invalidCharacters);
        LOG.error(INVALID_MODULE_NAME.logFormat(name, message));
        throw new SystemConfigException(INVALID_MODULE_NAME.logFormat(name, message));
    }
}

From source file:ai.grakn.migration.csv.Main.java

public static void runCSV(MigrationCLI cli) {
    String csvDataFileName = cli.getRequiredOption("input", "Data file missing (-i)");
    String csvTemplateName = cli.getRequiredOption("template", "Template file missing (-t)");
    int batchSize = cli.hasOption("b") ? Integer.valueOf(cli.getOption("b")) : AbstractMigrator.BATCH_SIZE;
    String delimiterString = cli.hasOption("s") ? cli.getOption("s")
            : Character.toString(CSVMigrator.SEPARATOR);

    if (delimiterString.toCharArray().length != 1) {
        cli.die("Wrong number of characters in delimiter " + delimiterString);
    }/*from w  ww .  ja va 2s  .  c  o m*/

    char csvDelimiter = delimiterString.toCharArray()[0];

    // get files
    File csvDataFile = new File(csvDataFileName);
    File csvTemplate = new File(csvTemplateName);

    if (!csvTemplate.exists()) {
        cli.die("Cannot find file: " + csvTemplateName);
    }

    if (!csvDataFile.exists()) {
        cli.die("Cannot find file: " + csvDataFileName);
    }

    cli.printInitMessage(csvDataFile.getPath());

    String template = cli.fileAsString(csvTemplate);
    try (CSVMigrator csvMigrator = new CSVMigrator(template, csvDataFile).setSeparator(csvDelimiter)) {

        if (cli.hasOption("n")) {
            cli.writeToSout(csvMigrator.migrate());
        } else {
            MigrationLoader.load(cli.getLoader(), batchSize, csvMigrator);
            cli.printWholeCompletionMessage();
        }
    } catch (Throwable throwable) {
        cli.die(throwable);
    }

    cli.initiateShutdown();
}

From source file:Main.java

/**
 * Resolve reserved XML characters//from   w w w  .  j  a  v  a  2 s . c om
 *  
 */
public static String filterSpecialChars(String in) {
    if (in == null)
        return null;

    StringBuffer sb = new StringBuffer();
    char[] chars = in.toCharArray();

    for (int i = 0; i < chars.length; i++) {

        switch (chars[i]) {
        case '<':
            sb.append("&lt;");
            break;

        case '>':
            sb.append("&gt;");
            break;

        case '&':
            sb.append("&amp;");
            break;

        default:
            sb.append(chars[i]);
        }
    }

    return sb.toString();
}

From source file:boa.BoaMain.java

protected static String pascalCase(final String string) {
    final StringBuilder pascalized = new StringBuilder();

    boolean upper = true;
    for (final char c : string.toCharArray())
        if (Character.isDigit(c) || c == '_') {
            pascalized.append(c);//ww  w .  j  a  va2 s  .  c om
            upper = true;
        } else if (!Character.isDigit(c) && !Character.isLetter(c)) {
            upper = true;
        } else if (Character.isLetter(c)) {
            pascalized.append(upper ? Character.toUpperCase(c) : c);
            upper = false;
        }

    return pascalized.toString();
}

From source file:com.anteam.demo.codec.cipher.symmetric.DESTest.java

License:asdf

public static byte[] stringToBytes(String string) {

    char[] chars = string.toCharArray();

    byte[] bytes = new byte[chars.length];
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = (byte) chars[i];
    }// w  w  w. j a  va2 s  .  c o  m
    return bytes;
}