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 String convertToQuotedString(String string) {
    if (TextUtils.isEmpty(string))
        return "";

    final int lastPos = string.length() - 1;
    if (lastPos < 0 || (string.charAt(0) == '"' && string.charAt(lastPos) == '"')) {
        return string;
    }/* w  w w  .j a va  2 s.  c om*/

    return "\"" + string + "\"";
}

From source file:Main.java

/**
 * A deliberately very inflexible camel case to underscored converter; it must not convert improper camel case
 * names to a proper underscored name./*from   w  w  w .  ja  v  a  2s  .c om*/
 */
public static String camelCaseToUnderscored(String camelCaseName) {
    int i = 0;
    while (i < camelCaseName.length() && Character.isLowerCase(camelCaseName.charAt(i))) {
        i++;
    }
    if (i == camelCaseName.length()) {
        // No conversion needed
        return camelCaseName;
    }

    StringBuffer sb = new StringBuffer();
    sb.append(camelCaseName.substring(0, i));
    while (i < camelCaseName.length()) {
        final char c = camelCaseName.charAt(i);
        if (isUpperUSASCII(c)) {
            sb.append('_');
            sb.append(Character.toLowerCase(c));
        } else {
            sb.append(c);
        }
        i++;
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Determine if the current s is an integer or not
 * /* w  ww.  j  av a 2  s  .  c  om*/
 * @param s
 * @return
 */
public static boolean isInteger(String s) {
    if (s == null || s.length() == 0)
        return false;
    for (int i = 0; i < s.length(); i++) {
        if (i == 0 && s.charAt(i) == '-')
            continue;
        if (s.charAt(i) > '9' || s.charAt(i) < '0')
            return false;
    }
    return true;
}

From source file:Strings.java

/**
 * Returns a title-cased version of the specified word,
 * which involves capitalizing the first character in
 * the word if it is a letter.//from   w  w w  .  j a  va2  s.  co  m
 *
 * @param word The word to convert to title case.
 * @return Title cased version of specified word.
 */
public static String titleCase(String word) {
    if (word.length() < 1)
        return word;
    if (!Character.isLetter(word.charAt(0)))
        return word;
    return Character.toUpperCase(word.charAt(0)) + word.substring(1);
}

From source file:Main.java

public static boolean isNumeric(String str) {
    if (str == null) {
        return false;
    }//from   w  ww.ja  va2  s.c  o  m
    for (int i = str.length(); --i >= 0;) {
        if (!Character.isDigit(str.charAt(i))) {
            return false;
        }
    }
    return true;
}

From source file:StandardUtilities.java

/**
 * @param str A java string//  ww  w.j av  a  2 s .c  om
   * @return the leading whitespace of that string, for indenting subsequent lines.
 * @since jEdit 4.3pre10
 */
public static String getIndentString(String str) {

    StringBuilder indentString = new StringBuilder();
    int idx = 0;
    char ch = str.charAt(idx);
    while (idx < str.length() && Character.isWhitespace(ch)) {
        indentString.append(ch);
        idx++;
        ch = str.charAt(idx);
    }
    return indentString.toString();

}

From source file:Main.java

public static byte[] toBytesFromUnicode(String s) {
    int limit = s.length() * 2;
    byte[] result = new byte[limit];
    char c;/*from  w  w w.j  a  v a  2 s  . c om*/
    for (int i = 0; i < limit; i++) {
        c = s.charAt(i >>> 1);
        result[i] = (byte) (((i & 1) == 0) ? c >>> 8 : c);
    }
    return result;
}

From source file:Main.java

public static String extractAttribute(String input, String attribute) {
    String[] split = input.split(attribute + "=");
    if (split.length == 1) {
        split = input.split(attribute + " =");
    }//from w  ww . ja v  a2s . co  m
    if (split.length > 1) {
        String lastPart = split[1];
        if (lastPart == null) {
            return null;
        }
        if (lastPart.charAt(0) == '"' || lastPart.charAt(0) == '\'') {
            int endIndex = -1;
            for (int i = 1; i < lastPart.length(); i++) {
                if (lastPart.charAt(i) == '\'' || lastPart.charAt(i) == '"') {
                    endIndex = i;
                    break;
                }
            }

            if (endIndex > 0) {
                return lastPart.substring(1, endIndex);
            }

        }
    }
    return null;

}

From source file:Main.java

public static String escape(String str) {
    String res = "";
    for (int i = 0; i < str.length(); i++) {
        final char c = str.charAt(i);
        switch (c) {
        case '<':
            res += "&lt;";
            break;
        case '>':
            res += "&gt;";
            break;
        case '&':
            res += "&amp;";
            break;
        case '\'':
            res += "&apos;";
            break;
        case '"':
            res += "&quot;";
            break;
        default:/*from   w w  w  . j av a2  s . c  om*/
            res += c;
        }
    }
    return res;
}

From source file:helpers.OpenBisFunctions.java

/**
 * Returns the 4 or 5 character project prefix used for samples in openBIS.
 * /*www .java  2 s. c o m*/
 * @param sample sample ID starting with a standard project prefix.
 * @return Project prefix of the sample
 */
public static String getProjectPrefix(String sample) {
    if (Utils.isInteger("" + sample.charAt(4)))
        return sample.substring(0, 4);
    else
        return sample.substring(0, 5);
}