Example usage for java.lang StringBuilder charAt

List of usage examples for java.lang StringBuilder charAt

Introduction

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

Prototype

char charAt(int index);

Source Link

Document

Returns the char value at the specified index.

Usage

From source file:Main.java

/**
 * Converts a string to title casing./* w ww.ja  va2 s  . c  o m*/
 * @param str
 *      The string to convert.
 * @return
 *      The converted string.
 */
public static String toTitleCase(String str) {
    if (str == null) {
        return null;
    }
    // skip if already contains mixed case
    if (!str.equals(str.toLowerCase()) && !str.equals(str.toUpperCase())) {
        return str;
    }

    boolean isSeparator = true;
    StringBuilder builder = new StringBuilder(str);
    final int len = builder.length();

    for (int i = 0; i < len; ++i) {
        char c = builder.charAt(i);
        if (isSeparator) {
            if (Character.isLetterOrDigit(c)) {
                // Convert to title case and switch out of whitespace mode.
                builder.setCharAt(i, Character.toTitleCase(c));
                isSeparator = false;
            }
        } else if (!Character.isLetterOrDigit(c)) {
            isSeparator = true;
        } else {
            builder.setCharAt(i, Character.toLowerCase(c));
        }
    }

    return builder.toString();
}

From source file:Main.java

/**
 * Trim <i>all</i> whitespace from the given String:
 * leading, trailing, and in between characters.
 * @param str the String to check/*from w  w  w .  j a va 2  s.c  om*/
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimAllWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuilder sb = new StringBuilder(str);
    int index = 0;
    while (sb.length() > index) {
        if (Character.isWhitespace(sb.charAt(index))) {
            sb.deleteCharAt(index);
        } else {
            index++;
        }
    }
    return sb.toString();
}

From source file:asia.gkc.vneedu.utils.FilterUtil.java

/**
 * ????//from  ww w. ja  v a  2  s  . c o  m
 * @param name - ????
 * @param prefix - ?,get/set
 * @param delimiter - ??
 * @return ??
 */
public static String getMethodName(String name, String prefix, String delimiter) {
    String[] parts = name.split(delimiter == null ? "_" : delimiter);
    StringBuilder sbOut = new StringBuilder(prefix == null ? "get" : prefix);

    if (parts.length >= 1) {
        for (String str : parts) {
            StringBuilder sb = new StringBuilder(str);
            sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
            sbOut.append(sb);
        }
    }

    return new String(sbOut);
}

From source file:Main.java

/**
 * Converts a string to title casing.//from  w  w w. j a v a2s.c o m
 *
 * @param str The string to convert.
 * @return The converted string.
 */
public static String toTitleCase(String str) {
    if (str == null) {
        return null;
    }

    boolean isSeparator = true;
    StringBuilder builder = new StringBuilder(str);
    final int len = builder.length();

    for (int i = 0; i < len; ++i) {
        char c = builder.charAt(i);
        if (isSeparator) {
            if (Character.isLetterOrDigit(c)) {
                // Convert to title case and switch out of whitespace mode.
                builder.setCharAt(i, Character.toTitleCase(c));
                isSeparator = false;
            }
        } else if (!Character.isLetterOrDigit(c)) {
            isSeparator = true;
        } else {
            builder.setCharAt(i, Character.toLowerCase(c));
        }
    }

    return builder.toString();
}

From source file:org.commonjava.aprox.dotmaven.util.NameUtils.java

public static String appendPath(final String base, final String... parts) {
    final StringBuilder sb = new StringBuilder();
    sb.append(base);/*from   w  w w .j  av a  2s. c  om*/
    for (final String part : parts) {
        if (sb.charAt(sb.length() - 1) == '/') {
            sb.setLength(sb.length() - 1);
        }

        if (!part.startsWith("/")) {
            sb.append("/");
        }

        sb.append(part);
    }

    return sb.toString();
}

From source file:io.wcm.sling.commons.util.Escape.java

/**
 * Creates a valid node name. Replaces all chars not in a-z, A-Z and 0-9 or '_' with '-' and converts all to lowercase.
 * @param value String to be labelized./*w w w .j a va  2  s .  co  m*/
 * @return The labelized string.
 */
public static String validName(String value) {

    // convert to lowercase
    String text = value.toLowerCase();

    // replace some special chars first
    text = StringUtils.replace(text, "", "ae");
    text = StringUtils.replace(text, "", "oe");
    text = StringUtils.replace(text, "", "ue");
    text = StringUtils.replace(text, "", "ss");

    // replace all invalid chars
    StringBuilder sb = new StringBuilder(text);
    for (int i = 0; i < sb.length(); i++) {
        char ch = sb.charAt(i);
        if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')
                || (ch == '_'))) {
            ch = '-';
            sb.setCharAt(i, ch);
        }
    }
    return sb.toString();
}

From source file:Main.java

public static String paramsmap2ParamsString(Map<String, String> params) {
    if (params == null)
        return "";
    StringBuilder sb = new StringBuilder();
    for (Map.Entry<String, String> param : params.entrySet()) {
        sb.append(param.getKey()).append("=").append(param.getValue()).append("&");
    }//from  ww w .  j  ava 2  s .c  om
    if (sb.charAt(sb.length() - 1) == '&')
        sb.deleteCharAt(sb.length() - 1);
    return sb.toString();
}

From source file:Main.java

public static String convertMaskedPhone(String phone) {
    StringBuilder phoneNumber = new StringBuilder(phone);
    for (int i = 0; i < phoneNumber.length(); i++) {
        switch (Character.toLowerCase(phoneNumber.charAt(i))) {
        case 'a':
        case 'b':
        case 'c':
            phoneNumber.setCharAt(i, '2');
            break;
        case 'd':
        case 'e':
        case 'f':
            phoneNumber.setCharAt(i, '3');
            break;
        case 'g':
        case 'h':
        case 'i':
            phoneNumber.setCharAt(i, '4');
            break;
        case 'j':
        case 'k':
        case 'l':
            phoneNumber.setCharAt(i, '5');
            break;
        case 'm':
        case 'n':
        case 'o':
            phoneNumber.setCharAt(i, '6');
            break;
        case 'p':
        case 'q':
        case 'r':
        case 's':
            phoneNumber.setCharAt(i, '7');
            break;
        case 't':
        case 'u':
        case 'v':
            phoneNumber.setCharAt(i, '8');
            break;
        case 'w':
        case 'x':
        case 'y':
        case 'z':
            phoneNumber.setCharAt(i, '9');
            break;
        case '+':
            phoneNumber.setCharAt(i, '0');
            break;
        }/*from  ww w.  ja  v  a2s  . co m*/
    }
    return phoneNumber.toString();
}

From source file:Main.java

public static String replaceEntities(String s) {
    StringBuilder sb = new StringBuilder(s);
    int len = s.length();
    int skip = 0;
    int iSkip = 0;
    for (int i = 0; i < len; i++) {
        iSkip = i + skip;/*from   ww w.j  a  v  a  2 s  . c o  m*/
        char c = sb.charAt(iSkip);
        if (c == '&') {
            sb.replace(iSkip, iSkip + 1, "&amp;");
            skip += 4;
        } else if (c == '\'') {
            sb.replace(iSkip, iSkip + 1, "&apos;");
            skip += 5;
        } else if (c == '<') {
            sb.replace(iSkip, iSkip + 1, "&lt;");
            skip += 3;
        } else if (c == '>') {
            sb.replace(iSkip, iSkip + 1, "&gt;");
            skip += 3;
        } else if (c == '"') {
            sb.replace(iSkip, iSkip + 1, "&quot;");
            skip += 5;
        }
    }
    return sb.toString();
}

From source file:Main.java

public static String formatValue(long value) throws NumberFormatException {
    if (value < 0) {
        throw new NumberFormatException("Negative value " + value);
    }/*from   w ww  .  j  a va2  s  .c  o  m*/
    StringBuilder sb = new StringBuilder(Long.toString(value));
    while (sb.length() <= 8) {
        sb.insert(0, '0');
    }
    sb.insert(sb.length() - 8, '.');
    while (sb.length() > 1 && (sb.charAt(sb.length() - 1) == '0' || sb.charAt(sb.length() - 1) == '.')) {
        sb.setLength(sb.length() - 1);
    }
    return sb.toString();
}