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

public static Boolean _isItReadable(String input) {
    int readableChar = 0;
    for (int i = 0; i < input.length(); i++) {
        int c = input.charAt(i);
        if (c >= 32 && c < 127) {
            readableChar++;//from w ww.j av  a 2 s .  co m
        }
    }

    // can be considered readable if X% characters are ascii
    // (0 is considered a character here so that UTF16 
    // can be considered readable too)
    return (readableChar > (input.length() * 0.75) ? true : false);
}

From source file:Main.java

public static int getUTFSize(String s) {

    int len = (s == null) ? 0 : s.length();
    int l = 0;/*from   www.  j a  v  a2  s. co m*/
    for (int i = 0; i < len; i++) {
        int c = s.charAt(i);
        if ((c >= 0x0001) && (c <= 0x007F)) {
            l++;
        } else if (c > 0x07FF) {
            l += 3;
        } else {
            l += 2;
        }
    }
    return l;
}

From source file:Main.java

public static void writeEscapedString(Writer writer, String value) throws IOException {
    for (int i = 0; i < value.length(); i++) {
        char c = value.charAt(i);

        if ((c >= ' ') && (c < 0x7f)) {
            if ((c == '\'') || (c == '\"') || (c == '\\')) {
                writer.write('\\');
            }//from   w w  w . ja  va  2 s.com
            writer.write(c);
            continue;
        } else if (c <= 0x7f) {
            switch (c) {
            case '\n':
                writer.write("\\n");
                continue;
            case '\r':
                writer.write("\\r");
                continue;
            case '\t':
                writer.write("\\t");
                continue;
            }
        }

        writer.write("\\u");
        writer.write(Character.forDigit(c >> 12, 16));
        writer.write(Character.forDigit((c >> 8) & 0x0f, 16));
        writer.write(Character.forDigit((c >> 4) & 0x0f, 16));
        writer.write(Character.forDigit(c & 0x0f, 16));
    }
}

From source file:Main.java

public static String parGetName(String fieldName) {
    if (null == fieldName || "".equals(fieldName)) {
        return null;
    }/*  w  w  w  .j av a2  s  . com*/
    int startIndex = 0;
    if (fieldName.charAt(0) == '_')
        startIndex = 1;
    return "get" + fieldName.substring(startIndex, startIndex + 1).toUpperCase()
            + fieldName.substring(startIndex + 1);
}

From source file:Main.java

private static boolean isFileProtocol(String href) {
    // Test for file: or /file:
    int index = href.indexOf("file:"); //$NON-NLS-1$
    return (index == 0 || (index == 1 && href.charAt(0) == '/'));
}

From source file:Main.java

public static String add0(String data) {
    String ss = "";
    if (data != null) {
        for (int i = 0; i < data.length(); i++) {
            ss += "0" + data.charAt(i);
        }//w  w  w  .ja  v  a  2  s  .c  o m
    } else
        return null;

    return ss;

}

From source file:Main.java

public static String xmlize(String s) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < s.length(); ++i) {
        char c = s.charAt(i);
        switch (c) {
        case '&':
            sb.append("&amp;");
            break;
        case '<':
            sb.append("&laquo;");
            break;
        case '>':
            sb.append("&raquo;");
            break;
        case '"':
            sb.append("&quot;");
            break;
        default://from ww  w. ja v  a 2 s .com
            sb.append(c);
        }
    }
    return sb.toString();
}

From source file:Main.java

static Map<String, Method> getGetPropertyMethods(Class<?> beanClass) {
    HashMap<String, Method> props;
    if (getPropsCache.containsKey(beanClass)) {
        props = getPropsCache.get(beanClass);
    } else {/* w  w w. ja v  a 2 s .co  m*/
        props = new HashMap<String, Method>();
        getPropsCache.put(beanClass, props);
        Method[] ms = beanClass.getMethods();
        for (int i = 0; i < ms.length; i++) {
            Method m = ms[i];
            String name = m.getName();
            if (name.startsWith("get") && name.length() > 3 && Character.isUpperCase(name.charAt(3))) {
                name = name.substring(3);
                props.put(name, m);
            }
        }
    }
    return props;
}

From source file:Main.java

public static String toUtf8String(String s) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (c >= 0 && c <= 255) {
            sb.append(c);//from w  w w . j a  va2  s.c o  m
        } else {
            byte[] b;
            try {
                b = String.valueOf(c).getBytes("utf-8");
            } catch (Exception ex) {
                System.out.println(ex);
                b = new byte[0];
            }
            for (int j = 0; j < b.length; j++) {
                int k = b[j];
                if (k < 0)
                    k += 256;
                sb.append("%" + Integer.toHexString(k).toUpperCase());
            }
        }
    }
    return sb.toString();
}

From source file:Main.java

/**
 * @param source/*from  w w  w  . ja v a  2  s.com*/
 * @param name
 * @return
 */
private static int getTagPos(String source, String name) {
    int pos = -1;
    if (source == null || source.isEmpty() || name == null || name.isEmpty())
        return pos;

    int start;
    String temp = source;

    while ((start = temp.indexOf(name)) >= 0) {
        temp = temp.substring(start);
        if (temp.charAt(name.length()) == ' ' || temp.charAt(name.length()) == '=')
            return start;

        temp = temp.substring(name.length());
    }

    return pos;
}