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:org.apdplat.platform.action.ExtJSSimpleAction.java

/**
 * @param src//from w w  w. ja  v a 2  s .c o  m
 * ?
 * @return src??srcnull
 */
public static String change(String src) {
    if (src != null) {
        StringBuilder sb = new StringBuilder(src);
        sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
        return sb.toString();
    } else {
        return null;
    }
}

From source file:com.thoughtworks.go.util.SelectorUtils.java

/**
 * removes from a pattern all tokens to the right containing wildcards
 * @param input the input string/* ww  w .j a  v  a 2s  . co  m*/
 * @return the leftmost part of the pattern without wildcards
 */
public static String rtrimWildcardTokens(String input) {
    String[] tokens = tokenizePathAsArray(input);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < tokens.length; i++) {
        if (hasWildcards(tokens[i])) {
            break;
        }
        if (i > 0 && sb.charAt(sb.length() - 1) != File.separatorChar) {
            sb.append(File.separatorChar);
        }
        sb.append(tokens[i]);
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Used for flattening a collection of objects into a string
 * @param   array     Array of elements to flatten
 * @param   fmt       Format string to use for array flattening
 * @param   separator Separator to use for string concat
 * @return  Representative string made up of array elements
 *//*from w  w  w.  jav  a2s .co m*/
private static String flattenCollection(String elemName, Collection<?> array, String fmt, char separator) {
    StringBuilder builder = new StringBuilder();

    //append all elements in the array into a string
    for (Object element : array) {
        String elemValue = null;

        //replace null values with empty string to maintain index order
        if (null == element) {
            elemValue = "";
        } else if (element instanceof Date) {
            elemValue = dateToString((Date) element);
        } else {
            elemValue = element.toString();
        }
        elemValue = tryUrlEncode(elemValue);
        builder.append(String.format(fmt, elemName, elemValue, separator));
    }

    //remove the last separator, if appended
    if ((builder.length() > 1) && (builder.charAt(builder.length() - 1) == separator))
        builder.deleteCharAt(builder.length() - 1);

    return builder.toString();
}

From source file:com.abstratt.mdd.internal.frontend.textuml.TextUMLFormatter.java

private static boolean isAt(StringBuilder output, char... chars) {
    if (output.length() <= 0)
        return false;
    for (char c : chars)
        if (output.charAt(output.length() - 1) == c)
            return true;
    return false;
}

From source file:org.commonjava.web.json.test.WebFixture.java

public static String buildUrl(final String baseUrl, final Map<String, String> params, final String... parts)
        throws MalformedURLException {
    if (parts == null || parts.length < 1) {
        return baseUrl;
    }//from  w  ww . java 2  s. co  m

    final StringBuilder urlBuilder = new StringBuilder();

    if (!parts[0].startsWith(baseUrl)) {
        urlBuilder.append(baseUrl);
    }

    for (String part : parts) {
        if (part.startsWith("/")) {
            part = part.substring(1);
        }

        if (urlBuilder.length() > 0 && urlBuilder.charAt(urlBuilder.length() - 1) != '/') {
            urlBuilder.append("/");
        }

        urlBuilder.append(part);
    }

    if (params != null && !params.isEmpty()) {
        urlBuilder.append("?");
        boolean first = true;
        for (final Map.Entry<String, String> param : params.entrySet()) {
            if (first) {
                first = false;
            } else {
                urlBuilder.append("&");
            }

            urlBuilder.append(param.getKey()).append("=").append(param.getValue());
        }
    }

    return new URL(urlBuilder.toString()).toExternalForm();
}

From source file:com.github.abel533.mapperhelper.EntityHelper.java

/**
 * ?/*from   ww  w. j  av  a2 s .c o  m*/
 */
public static String underlineToCamelhump(String str) {
    Matcher matcher = Pattern.compile("_[a-z]").matcher(str);
    StringBuilder builder = new StringBuilder(str);
    for (int i = 0; matcher.find(); i++) {
        builder.replace(matcher.start() - i, matcher.end() - i, matcher.group().substring(1).toUpperCase());
    }
    if (Character.isUpperCase(builder.charAt(0))) {
        builder.replace(0, 1, String.valueOf(Character.toLowerCase(builder.charAt(0))));
    }
    return builder.toString();
}

From source file:cn.org.awcp.core.mybatis.mapper.EntityHelper.java

/**
 * ?/*from w w  w  .  jav  a 2 s. c  o  m*/
 */
public static String camelhumpToUnderline(String str) {
    final int size;
    final char[] chars;
    final StringBuilder sb = new StringBuilder((size = (chars = str.toCharArray()).length) * 3 / 2 + 1);
    char c;
    for (int i = 0; i < size; i++) {
        c = chars[i];
        if (isUppercaseAlpha(c)) {
            sb.append('_').append(c);
        } else {
            sb.append(toUpperAscii(c));
        }
    }
    return sb.charAt(0) == '_' ? sb.substring(1) : sb.toString();
}

From source file:com.medallia.tiny.Strings.java

/** Does exactly what you think */
public static String rot13(String s) {
    StringBuilder sb = new StringBuilder(s);
    for (int i = 0; i < sb.length(); i++) {
        char c = sb.charAt(i);

        if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M')) {
            c += 13;/*from w w  w .  j a  v  a2  s . c  om*/
        } else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z')) {
            c -= 13;
        }

        sb.setCharAt(i, c);
    }
    return sb.toString();
}

From source file:com.marklogic.contentpump.utilities.OptionsFileUtil.java

/**
 * Expands any options file that may be present in the given set of arguments.
 *
 * @param args the given arguments/*from w  w w.  j  av  a2s .  c o  m*/
 * @return a new string array that contains the expanded arguments.
 * @throws Exception
 */
public static String[] expandArguments(String[] args) throws Exception {
    List<String> options = new ArrayList<String>();

    for (int i = 0; i < args.length; i++) {
        if (args[i].equals(OPTIONS_FILE)) {
            if (i == args.length - 1) {
                throw new Exception("Missing options file");
            }

            String fileName = args[++i];
            File optionsFile = new File(fileName);
            BufferedReader reader = null;
            StringBuilder buffer = new StringBuilder();
            try {
                reader = new BufferedReader(new FileReader(optionsFile));
                String nextLine = null;
                while ((nextLine = reader.readLine()) != null) {
                    nextLine = nextLine.trim();
                    if (nextLine.length() == 0 || nextLine.startsWith("#")) {
                        // empty line or comment
                        continue;
                    }

                    buffer.append(nextLine);
                    if (nextLine.endsWith("\\")) {
                        if (buffer.charAt(0) == '\'' || buffer.charAt(0) == '"') {
                            throw new Exception("Multiline quoted strings not supported in file(" + fileName
                                    + "): " + buffer.toString());
                        }
                        // Remove the trailing back-slash and continue
                        buffer.deleteCharAt(buffer.length() - 1);
                    } else {
                        // The buffer contains a full option
                        options.add(removeQuotesEncolosingOption(fileName, buffer.toString()));
                        buffer.delete(0, buffer.length());
                    }
                }

                // Assert that the buffer is empty
                if (buffer.length() != 0) {
                    throw new Exception(
                            "Malformed option in options file(" + fileName + "): " + buffer.toString());
                }
            } catch (IOException ex) {
                throw new Exception("Unable to read options file: " + fileName, ex);
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException ex) {
                        LOG.info("Exception while closing reader", ex);
                    }
                }
            }
        } else {
            // Regular option. Parse it and put it on the appropriate list
            options.add(args[i]);
        }
    }

    return options.toArray(new String[options.size()]);
}

From source file:com.cinchapi.concourse.shell.SyntaxTools.java

/**
 * Check {@code line} to see if it is a function call that is missing any
 * commas among arguments.//from  ww  w. j  a v a 2s  .  c o m
 * 
 * @param line
 * @param methods
 * @return the line with appropriate argument commas
 */
public static String handleMissingArgCommas(String line, List<String> methods) {
    int hashCode = methods.hashCode();
    Set<String> hashedMethods = CACHED_METHODS.get(hashCode);
    if (hashedMethods == null) {
        hashedMethods = Sets.newHashSetWithExpectedSize(methods.size());
        hashedMethods.addAll(methods);
        CACHED_METHODS.put(hashCode, hashedMethods);
    }
    char[] chars = line.toCharArray();
    StringBuilder transformed = new StringBuilder();
    StringBuilder gather = new StringBuilder();
    boolean foundMethod = false;
    boolean inSingleQuotes = false;
    boolean inDoubleQuotes = false;
    int parenCount = 0;
    for (char c : chars) {
        if (Character.isWhitespace(c) && !foundMethod) {
            transformed.append(gather);
            transformed.append(c);
            foundMethod = hashedMethods.contains(gather.toString());
            gather.setLength(0);
        } else if (Character.isWhitespace(c) && foundMethod) {
            if (transformed.charAt(transformed.length() - 1) != ',' && !inSingleQuotes && !inDoubleQuotes
                    && c != '\n') {
                transformed.append(",");
            }
            transformed.append(c);
        } else if (c == '(' && !foundMethod) {
            parenCount++;
            transformed.append(gather);
            transformed.append(c);
            foundMethod = hashedMethods.contains(gather.toString());
            gather.setLength(0);
        } else if (c == '(' && foundMethod) {
            parenCount++;
            transformed.append(c);
        } else if (c == ';') {
            transformed.append(c);
            foundMethod = false;
            parenCount = 0;
        } else if (c == ')') {
            parenCount--;
            transformed.append(c);
            foundMethod = parenCount == 0 ? false : foundMethod;
        } else if (c == '"') {
            transformed.append(c);
            inSingleQuotes = !inSingleQuotes;
        } else if (c == '\'') {
            transformed.append(c);
            inDoubleQuotes = !inDoubleQuotes;
        } else if (foundMethod) {
            transformed.append(c);
        } else {
            gather.append(c);
        }
    }
    transformed.append(gather);
    return transformed.toString();
}