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:Main.java

/**
 * Check whether the given String is a valid identifier according
 * to the Java Language specifications.//w  w  w  . j av a 2  s.  c o m
 *
 * See The Java Language Specification Second Edition, Section 3.8
 * for the definition of what is a valid identifier.
 *
 * @param s String to check
 *
 * @return <code>true</code> if the given String is a valid Java
 *         identifier, <code>false</code> otherwise.
 */
public final static boolean isValidJavaIdentifier(String s) {
    // an empty or null string cannot be a valid identifier
    if (s == null || s.length() == 0) {
        return false;
    }

    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:Main.java

/**
 * Get characters' unicode in hexadecimal.
 *//* w  w w .j  av a  2s. c o m*/
public static String getUnicode(String text, int len_limit) {
    if (TextUtils.isEmpty(text) || len_limit <= 0) {
        return "";
    }

    StringBuilder stringBuilder = new StringBuilder();

    final char[] CHARS = text.toCharArray();
    for (int len = CHARS.length, i = len - 1; i >= 0; --i) {
        if (len - i <= len_limit) {
            stringBuilder.insert(0, Integer.toHexString(CHARS[i]).toUpperCase());
            stringBuilder.insert(0, " ");
        } else {
            stringBuilder.insert(0, " ...");
            break;
        }
    }

    //Remove the first superfluous " ".
    return stringBuilder.substring(1);
}

From source file:Main.java

public static byte[] hexStr2Bytes(String hexString) {
    if (hexString == null || hexString.equals("")) {
        return null;
    }/*from ww w .ja v a 2 s .co m*/
    hexString = hexString.toUpperCase();
    int length = hexString.length() / 2;
    char[] hexChars = hexString.toCharArray();
    byte[] d = new byte[length];
    for (int i = 0; i < length; i++) {
        int pos = i * 2;
        d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
    }
    return d;
}

From source file:com.open.cas.shiro.util.EncodeUtils.java

/**
 * Hex?./*from  w w w. j a va  2 s.c om*/
 */
public static byte[] decodeHex(String input) {
    try {
        return Hex.decodeHex(input.toCharArray());
    } catch (DecoderException e) {
        throw ExceptionUtils.unchecked(e);
    }
}

From source file:com.vvote.verifierlibrary.utils.Utils.java

/**
 * Decodes a string in hexadecimal format into byte format
 * /*www  .j  av a 2s .  c  om*/
 * @param data
 * @return byte data
 */
public static byte[] decodeHexData(String data) {
    try {
        return Hex.decodeHex(data.toCharArray());
    } catch (DecoderException e) {
        logger.error("Unable to decode hex data from string: {}", data);
    }

    return null;
}

From source file:net.jforum.util.URLNormalizer.java

  /**
 * /* w  w w  .ja v  a  2 s .c  om*/
 * @param url the url to normalize
 * @param limit do not process more than <code>limit + 1</code> chars
 * @param friendlyTruncate If <code>true</code>, will try to not cut a word if
 * more than <code>limit</code> chars were processed. It will stop in the next
 * special char
 * @return the normalized url
 */
public static String normalize(String url, int limit, boolean friendlyTruncate)
{
  char[] chars = url.toCharArray();
    
  StringBuffer sb = new StringBuffer(url.length());
    
  for (int i = 0; i < chars.length; i++) {
    if (i <= limit || (friendlyTruncate && i > limit && sb.charAt(sb.length() - 1) != '_')) {
        
      if (Character.isSpaceChar(chars[i]) || chars[i] == '-') {
        if (friendlyTruncate && i > limit) {
          break;
        }
          
        if (i > 0 && sb.charAt(sb.length() - 1) != '_') {
          sb.append('_');
        }
      }
        
      if (Character.isLetterOrDigit(chars[i])) {
        sb.append(chars[i]);
      }
      else if (friendlyTruncate && i > limit) {
        break;
      }
    }
  }
    
  return sb.toString().toLowerCase();
}

From source file:Main.java

private static String convert(String string) {
    if (string == null && string.trim().length() == 0)
        return "";

    StringBuffer buffer = new StringBuffer();
    final char[] characters = string.toCharArray();
    for (int i = 0; i < characters.length; i++) {
        switch (characters[i]) {
        case '<':
            buffer.append("&lt;");
            break;

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

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

        default:/*  w  w  w.j  a v a2  s.  c om*/
            buffer.append(characters[i]);
            break;
        }
    }
    return buffer.toString();
}

From source file:kr.co.bitnine.octopus.schema.metamodel.OctopusMetaModelDataContextFactory.java

public static DataContext createMongoDbDataContext(JSONObject jsonObject) {
    DataContext dc;/*w w w.j  a  v a2s . c  o m*/
    String host = (String) jsonObject.get("host");
    String port = (String) jsonObject.get("port");
    String database = (String) jsonObject.get("database");
    String userName = (String) jsonObject.get("user");
    String password = (String) jsonObject.get("password");

    dc = DataContextFactory.createMongoDbDataContext(host, Integer.parseInt(port), database, userName,
            password.toCharArray());
    return dc;
}

From source file:com.ruzhi.demo.lifeserverweb.StringUtil.java

/**
 * ? GBK? add by dangkang 2015-03//from w  w  w  . ja  va2s.  c  o  m
 * @param str 
 * @return boolean
 */
public static boolean checkGBKOnly(String str) {
    char[] chars = str.toCharArray();
    boolean isGBK = false;
    for (int i = 0; i < chars.length; i++) {
        byte[] bytes = ("" + chars[i]).getBytes();
        if (bytes.length != 2) {
            isGBK = false;
            break;
        }
        int[] ints = new int[2];
        ints[0] = bytes[0] & 0xff;
        ints[1] = bytes[1] & 0xff;
        if (ints[0] >= 0x81 && ints[0] <= 0xFE && ints[1] >= 0x40 && ints[1] <= 0xFE) {//128 64
            isGBK = true;
        } else {
            isGBK = false;
        }
    }

    return isGBK;
}

From source file:com.stg.crypto.CryptoHelper.java

/**
 * Convert the passed in String to a byte array by
 * taking the bottom 8 bits of each character it contains.
 * //from  ww  w . ja va 2  s  .c o m
 * @param string the string to be converted
 * @return a byte array representation
 */
public static byte[] toByteArray(String string) {
    byte[] bytes = new byte[string.length()];
    char[] chars = string.toCharArray();

    for (int i = 0; i != chars.length; i++) {
        bytes[i] = (byte) chars[i];
    }

    return bytes;
}