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:com.google.flightmap.parsing.util.StringUtils.java

/**
 * Returns mixed-case version of {@code text}.
 * <p>/*  www.  j av a2  s  .  c  o  m*/
 * The first letter of each word is capitalized, the rest are lower case.
 * Words are delimited by spaces and special characters (except single-quote).
 * "REID-HILLVIEW" becomes "Reid-Hillview".
 * 
 * @return mixed-case version of {@code text} with each word capitalized.
 */
public static String capitalize(final String text) {
    final StringBuilder sb = new StringBuilder(WordUtils.capitalize(text.toLowerCase()));
    boolean makeNextLetterUpper = false;
    for (int i = 0; i < sb.length(); ++i) {
        final char cur = sb.charAt(i);
        if (Character.isWhitespace(cur)) {
            continue; // Skip whitespace
        } else if (Character.isLetterOrDigit(cur)) {
            if (makeNextLetterUpper) {
                sb.setCharAt(i, Character.toUpperCase(cur));
                makeNextLetterUpper = false;
            } else {
                continue; // Skip character if no change is neded
            }
        } else { // Not whitespace, letter or digit:  we assume punctuation.
            makeNextLetterUpper = cur != '\''; // Ignore single quote (John'S, Susie'S, ...)
        }
    }
    return sb.toString();
}

From source file:net.certiv.antlr.project.util.Strings.java

public static String camelCase(String in) {
    StringBuilder sb = new StringBuilder(in);
    for (int idx = sb.length() - 1; idx >= 0; idx--) {
        char c = sb.charAt(idx);
        if (c == '_') {
            sb.deleteCharAt(idx);// w w w  .j ava  2  s .  co m
            sb.setCharAt(idx, Character.toUpperCase(sb.charAt(idx)));
        } else if (Character.isUpperCase(c)) {
            sb.setCharAt(idx, Character.toLowerCase(c));
        }
    }
    sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
    return sb.toString();
}

From source file:Main.java

/**
 * Trim all occurrences of the supplied leading character from the given String.
 * @param str the String to check//  ww  w  . j a v  a  2  s .  c o m
 * @param leadingCharacter the leading character to be trimmed
 * @return the trimmed String
 */
public static String trimLeadingCharacter(String str, char leadingCharacter) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuilder sb = new StringBuilder(str);
    while (sb.length() > 0 && sb.charAt(0) == leadingCharacter) {
        sb.deleteCharAt(0);
    }
    return sb.toString();
}

From source file:org.dawb.passerelle.common.utils.SubstituteUtils.java

/**
 * Reads a UTF-8 text file with or without a BOM.
 * //from  w  w  w.  jav a2s. c o m
 * @param contents
 * @param variables
 * @return
 * @throws Exception
 */
public static String substitute(final InputStream contents, final Map<String, String> variables)
        throws Exception {

    if (contents == null)
        return null;
    try {
        BufferedReader ir = new BufferedReader(new InputStreamReader(contents, "UTF-8"));
        int c;
        StringBuilder expand = new StringBuilder();
        final char[] buf = new char[4096];
        while ((c = ir.read(buf, 0, 4096)) > 0) {

            if (expand.length() > 0) {
                if (expand.charAt(0) == FileUtils.BOM) {
                    expand.delete(0, 0);
                }
            }

            expand.append(buf, 0, c);
        }
        return SubstituteUtils.substitute(expand.toString(), variables);

    } finally {
        contents.close();
    }
}

From source file:ca.hoogit.garagepi.Utils.Helpers.java

/**
 * Capitalize  the first letter in string
 *
 * @param toCapitalize String to be capitalized
 * @return Capitalized string/*from   w w w  . jav  a  2s . co m*/
 */
public static String capitalize(String toCapitalize) {
    if (toCapitalize.isEmpty()) {
        return "";
    }
    StringBuilder builder = new StringBuilder(toCapitalize.toLowerCase());
    builder.setCharAt(0, Character.toUpperCase(builder.charAt(0)));
    return builder.toString();
}

From source file:Main.java

/**
 * Trim leading whitespace from the given String.
 * @param str the String to check//from w ww. java 2  s  .c  om
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimLeadingWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuilder sb = new StringBuilder(str);
    while (sb.length() > 0 && Character.isWhitespace(sb.charAt(0))) {
        sb.deleteCharAt(0);
    }
    return sb.toString();
}

From source file:com.zuoxiaolong.niubi.job.persistent.hibernate.HibernateNamingStrategy.java

private static String addUnderscores(String name) {
    StringBuilder buf = new StringBuilder(name.replace('.', '_'));
    for (int i = 1; i < buf.length() - 1; i++) {
        if (Character.isLowerCase(buf.charAt(i - 1)) && Character.isUpperCase(buf.charAt(i))
                && Character.isLowerCase(buf.charAt(i + 1))) {
            buf.insert(i++, '_');
        }//from ww  w.j  av  a2 s .c  om
    }
    return buf.toString().toLowerCase(Locale.ROOT);
}

From source file:org.clickframes.util.ClickframeUtils.java

public static String lowerCaseFirstChar(String description) {
    StringBuilder s = new StringBuilder(description);
    s.setCharAt(0, Character.toLowerCase(s.charAt(0)));
    return s.toString();
}

From source file:com.linkedin.urls.PathNormalizer.java

License:asdf

/**
 * 1. Replaces "/./" with "/" recursively.
 * 2. "/blah/asdf/.." -> "/blah"// w  ww . java2  s  . co m
 * 3. "/blah/blah2/blah3/../../blah4" -> "/blah/blah4"
 * 4. "//" -> "/"
 * 5. Adds a slash at the end if there isn't one
 */
private static String sanitizeDotsAndSlashes(String path) {
    StringBuilder stringBuilder = new StringBuilder(path);
    Stack<Integer> slashIndexStack = new Stack<Integer>();
    int index = 0;
    while (index < stringBuilder.length() - 1) {
        if (stringBuilder.charAt(index) == '/') {
            slashIndexStack.add(index);
            if (stringBuilder.charAt(index + 1) == '.') {
                if (index < stringBuilder.length() - 2 && stringBuilder.charAt(index + 2) == '.') {
                    //If it looks like "/../" or ends with "/.."
                    if (index < stringBuilder.length() - 3 && stringBuilder.charAt(index + 3) == '/'
                            || index == stringBuilder.length() - 3) {
                        boolean endOfPath = index == stringBuilder.length() - 3;
                        slashIndexStack.pop();
                        int endIndex = index + 3;
                        // backtrack so we can detect if this / is part of another replacement
                        index = slashIndexStack.empty() ? -1 : slashIndexStack.pop() - 1;
                        int startIndex = endOfPath ? index + 1 : index;
                        stringBuilder.delete(startIndex + 1, endIndex);
                    }
                } else if (index < stringBuilder.length() - 2 && stringBuilder.charAt(index + 2) == '/'
                        || index == stringBuilder.length() - 2) {
                    boolean endOfPath = index == stringBuilder.length() - 2;
                    slashIndexStack.pop();
                    int startIndex = endOfPath ? index + 1 : index;
                    stringBuilder.delete(startIndex, index + 2); // "/./" -> "/"
                    index--; // backtrack so we can detect if this / is part of another replacement
                }
            } else if (stringBuilder.charAt(index + 1) == '/') {
                slashIndexStack.pop();
                stringBuilder.deleteCharAt(index);
                index--;
            }
        }
        index++;
    }

    if (stringBuilder.length() == 0) {
        stringBuilder.append("/"); //Every path has at least a slash
    }

    return stringBuilder.toString();
}

From source file:com.smartitengineering.version.api.factory.VersionAPI.java

/**
 * Throw away all the '/' from beginning and end of the resourceId.
 * @param resourceId Id to trim//from www.  j  a v a  2  s  .c om
 * @return Trimmed version of the resourceId
 */
public static String trimToProperResourceId(String resourceId) {
    if (StringUtils.isBlank(resourceId)) {
        return "";
    }
    StringBuilder result = new StringBuilder(resourceId.trim());
    while (result.charAt(0) == '/') {
        result.deleteCharAt(0);
    }
    while (result.charAt(result.length() - 1) == '/') {
        result.deleteCharAt(result.length() - 1);
    }
    return result.toString();
}