Example usage for java.lang String charAt

List of usage examples for java.lang String charAt

Introduction

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

Prototype

public char charAt(int index) 

Source Link

Document

Returns the char value at the specified index.

Usage

From source file:ReverseStringTest.java

public static String reverseIt(String source) {
    int i, len = source.length();
    StringBuffer dest = new StringBuffer(len);

    for (i = (len - 1); i >= 0; i--)
        dest.append(source.charAt(i));
    return dest.toString();
}

From source file:Main.java

public static HashMap tallyPrint(String phrase) {
    int count = 0;
    HashMap<Character, Integer> fav = new HashMap<Character, Integer>();
    for (int i = 0; i < phrase.length(); i++) {
        if (fav.containsKey(phrase.charAt(i)))
            fav.put(phrase.charAt(i), (fav.get(phrase.charAt(i))) + 1);
        else//from  ww  w .ja  v a  2  s . c o m
            fav.put(phrase.charAt(i), 1);
    }
    return fav;
}

From source file:Main.java

public static String getLastText(String text) {
    if (text == null) {
        return null;
    }/*  ww  w  .  ja v a2s .co m*/
    for (int i = text.length() - 1; i >= 0; --i) {
        int j = text.charAt(i);
        if ((j >= 19968) && (j <= 40869)) {
            return String.valueOf(j);
        }
    }
    return null;
}

From source file:Main.java

/**
 * @param str//from  www.  j  av  a2  s . com
 *            the string to encode
 * @return the corresponding encoded byte array
 */
public static byte[] stringToBytesUTFCustom(String str) {
    byte[] b = new byte[str.length() << 1];
    for (int i = 0; i < str.length(); i++) {
        char strChar = str.charAt(i);
        int bpos = i << 1;
        b[bpos] = (byte) ((strChar & 0xFF00) >> 8);
        b[bpos + 1] = (byte) (strChar & 0x00FF);
    }
    return b;
}

From source file:Main.java

/**
 * @param xml/*from w w w  . java 2 s.  c  o m*/
 * @param nameSpace
 */
private static String addNameSpaceToRootElement(final String xml, final String nameSpace) {
    int count = 0;
    int i = 0;
    while (count < 2) {
        if (xml.charAt(i) == '>') {
            count++;
        }
        i++;
    }
    String first = xml.substring(0, i - 1);
    String second = xml.substring(i, xml.length());
    return first + " " + nameSpace + ">" + second;

}

From source file:Main.java

public static int findChar(String s, char target, int offset) {
    int l = s.length();
    int i = offset;
    int found = -1;
    while (i < l) {
        if (s.charAt(i) == target) {
            found = i;/*w w  w  . ja  va  2 s  . c o  m*/
            i = l;
        }
        i++;
    }
    return found;
}

From source file:Main.java

/**
 * Removes invalid char from element names.
 *//*  w  w  w . j  a  va 2  s . co  m*/
public static String asElementName(String x) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < x.length(); ++i) {
        char c = x.charAt(i);
        if (Character.isUpperCase(c) || Character.isLowerCase(c) || Character.isDigit(c) || c == '-'
                || c == '_') {
            sb.append(c);
        }
    }
    String elementName = sb.toString();
    if (elementName.length() == 0 || (!Character.isUpperCase(elementName.charAt(0))
            && !Character.isLowerCase(elementName.charAt(0)))) {
        return "e" + elementName;
    }
    return elementName;
}

From source file:Main.java

public static String toTitleString(String s) {
    if (s.length() == 0) {
        return s;
    }//from  w w w . j  a  v a2 s  . c  om
    int idx = 0;
    char c = s.charAt(idx);
    if (!(c >= 'a' && c <= 'z')) {
        for (idx = 1; idx < s.length(); idx++) {
            if (c >= 'A' && c <= 'Z') {
                break;
            }
        }
    }
    if (idx == s.length()) {
        return s;
    }
    StringBuilder buf = new StringBuilder(s.substring(0, idx));
    if (idx == 0) {
        buf.append(toUpper(s.charAt(idx)));
        idx++;
    }
    for (; idx < s.length(); idx++) {
        buf.append(toLower(s.charAt(idx)));
    }
    return buf.toString();
}

From source file:Main.java

public final static String asUpperCaseFirstChar(final String target) {

    if ((target == null) || (target.length() == 0)) {
        return target;
    }/*from w  w  w  .  j ava2s .c  o  m*/
    return Character.toUpperCase(target.charAt(0)) + (target.length() > 1 ? target.substring(1) : "");
}

From source file:Main.java

/**
 * Converts a String SJIS or JIS URL encoded hex encoding to a Unicode String
 * //w  w  w.  j a v a2  s  . co m
 */
public static String convertJISEncoding(String target) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    if (target == null)
        return null;
    String paramString = target.trim();

    for (int loop = 0; loop < paramString.length(); loop++) {
        int i = (int) paramString.charAt(loop);
        bos.write(i);
    }
    String convertedString = null;
    try {
        convertedString = new String(bos.toByteArray(), "JISAutoDetect");
    } catch (java.io.UnsupportedEncodingException uex) {
    }
    return convertedString;
}