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 int calcLength(String s) {
    double len = 0;
    for (int i = 0; i < s.length(); i++) {
        int temp = (int) s.charAt(i);
        if (temp > 0 && temp < 127) {
            len += 0.5;/*from   ww  w . j  a va 2s . co  m*/
        } else {
            len++;
        }
    }
    return (int) len;
}

From source file:Main.java

/**
 * Strip trailing whitespace at end of line
 *
 * @param str//from  www .ja v  a 2  s. co  m
 * @return
 */
public static String rStrip(String str) {
    int last = str.length() - 1;
    int end = last;

    while ((end >= 0) && (str.charAt(end) <= ' ')) {
        end--;
    }
    if (end == last) {
        return str;
    }
    return str.substring(0, end + 1);
}

From source file:Main.java

/**
 * Return true if the string represent a number
 * in the specified radix./*from w w  w .  ja v  a 2 s .  c o m*/
 * <br><br>
 **/
public static boolean isNumeric(String s, int radix) {
    int i = 0, len = s.length();
    while (i < len && Character.digit(s.charAt(i), radix) > -1) {
        i++;
    }
    return (i >= len && len > 0);
}

From source file:Main.java

/**
 * Creates a Date object from the string passed to it. The passed string
 * needs to represent the date is W3C Date and Time Format
 * /*  w  ww .j  a va 2s .c o  m*/
 * @param date
 *            Source string containing date in W3C Date and Time Format
 * @return Date object containing the date extracted from the string it is
 *         passed
 * @throws ParseException
 *             If the begginning of <var>date</var> cannot be parsed
 */
public static Date dateFromW3CDTF(String date) throws ParseException {
    int len = date.length();
    if (len > 5 && date.charAt(len - 3) == ':') {
        char c = date.charAt(len - 6);
        if (c == '+' || c == '-')
            date = date.substring(0, len - 3) + date.substring(len - 2);
    }
    return (Date) w3cdtf.get().parseObject(date);
}

From source file:Main.java

public static double parseDouble(String s) throws NumberFormatException {
    boolean negative = (s.charAt(0) == '-'); // Check if negative symbol.
    double result = 0.0D; // Starting value
    int index = s.indexOf('.');

    if (index > -1) {
        // Means the decimal place exists, add values to right of it
        int divisor = 1;
        for (int i = index + 1; i < s.length(); i++) {
            divisor *= 10;/* ww w . ja v  a2s.  c om*/
            int curVal = (s.charAt(i) - 48); // Convert char to int
            if (curVal > 9 | curVal < 0)
                throw new NumberFormatException();
            result += ((double) curVal / divisor);
        }
    } else {
        index = s.length(); // If number string had no decimal
    }

    // Now add number characters to left of decimal
    int multiplier = 1;
    // TODO: Note: Sven removed a test here for no decimal place. Unsure if needed.
    // Check old version of SVN to see old line.
    int finish = negative ? 1 : 0; // Determine finishing position

    for (int i = index - 1; i >= finish; i--) {
        int curVal = (s.charAt(i) - 48); // Convert char to int
        if (curVal > 9 | curVal < 0)
            throw new NumberFormatException();
        result += (curVal * multiplier);
        multiplier *= 10;
    }

    return negative ? -result : result;
}

From source file:Main.java

public final static int getLength(String text) {
    int length = 0;
    for (int i = 0; i < text.length(); i++) {
        if (new String(text.charAt(i) + "").getBytes().length > 1) {
            length += 2;//from   w ww .  ja  v a2s  . c o  m
        } else {
            length += 1;
        }
    }
    return length / 2;
}

From source file:Main.java

public static int bernstein(String key) {
    int hash = 0;
    int i;//  w w w  . j  a  v a 2  s  .  com
    for (i = 0; i < key.length(); ++i)
        hash = 33 * hash + key.charAt(i);
    return hash;
}

From source file:Main.java

public static synchronized Method getSetter(final Class targetClass, final String propertyName) {
    String setterName = "set" + Character.toUpperCase(propertyName.charAt(0));
    if (setterName.length() > 1)
        setterName += propertyName.substring(1);

    final Method[] methods = targetClass.getMethods();

    for (Method m : methods) {
        if (m.getName().equals(setterName) && m.getParameterTypes().length == 1)
            return m;
    }//from   www  .  j  a  va  2 s .co  m
    return null;
}

From source file:Main.java

/**
 * Decode a variable. Variables are in the form $varname. If the incoming
 * expression is not a variable or is not recognised it is simply returned
 * verbatim./*from   w w w .ja va2s .c o  m*/
 * @param in The incoming attribute
 * @return String
 * @throws Exception
 */
private static String decode(String in) throws Exception {
    if (in != null && in.length() > 1 && in.charAt(0) == '$') {
        String key = in.substring(1);
        if (key.charAt(0) == '#') {
            // It's a class name and reflection job in the form $$full.class.name.member
            int lastIdx = key.lastIndexOf('.');
            String member = key.substring(lastIdx + 1);
            String className = key.substring(1, lastIdx);
            Class<?> clazz = Class.forName(className);
            Field field = clazz.getDeclaredField(member);
            field.setAccessible(true);
            return String.valueOf(field.get(null));
        } else {
            String val = vars.get(key);
            if (val == null) {
                throw new Exception("Unknown variable " + in);
            } else {
                return val;
            }
        }
    } else {
        return in;
    }
}

From source file:Main.java

/**
 * Return true if the string is alphanum.
 * <code>{letter digit (.) (_) (-) ( ) (?) }</code>
 * //from w  w  w . j  ava2s.  co  m
 **/
public static boolean isAlphaNumeric(String s) {
    int i = 0, len = s.length();
    while (i < len && (Character.isLetterOrDigit(s.charAt(i)) || s.charAt(i) == ' ' || s.charAt(i) == '.'
            || s.charAt(i) == '-' || s.charAt(i) == '_') || s.charAt(i) == '?') {
        i++;
    }
    return (i >= len);
}