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.conwet.xjsp.json.JSONUtil.java

/**
 * Extracts from the buffer a whole stanza or an stream finalizer "]}".
 * The recognized text is removed from the buffer.
 * /*from  ww w  .  java2  s . co m*/
 * Stanza recognition is implemented as a FSM. States:
 * <ul>
 *      <li>0. starting</li>
 *      <li>1. accepting text</li>
 *      <li>2. within an string</li>
 *      <li>3. finalizer</li>
 * </ul>
 * 
 * <img src="../../../../../resources/fsm.png"/>
 * 
 * @return Recognized text or <code>null</code>
 */
public static String extractStanza(StringBuilder buffer) throws ParseException {
    discardSpaces(buffer);

    int state = 0;
    int pos = 0;
    int level = 1;

    while (pos < buffer.length()) {
        char c = buffer.charAt(pos);
        switch (state) {
        case 0:
            switch (c) {
            case '{':
                state = 1;
                break;
            case ']':
                state = 3;
                break;
            default:
                throw new ParseException(ParseException.ERROR_UNEXPECTED_CHAR);
            }
            break;

        case 1:
            switch (c) {
            case '{':
                level++;
                break;
            case '}':
                level--;
                if (level == 0) {
                    String stanza = buffer.substring(0, pos + 1);
                    buffer.replace(0, pos + 1, "");
                    return stanza;
                }
                break;

            case '"':
                state = 2;
                break;

            default:
                // nothing
            }
            break;

        case 2:
            switch (c) {
            case '\\':
                pos++;
                break;

            case '"':
                state = 1;
                break;

            default:
                // nothing
            }
            break;

        case 3:
            if (isSpace(c)) {
                pos++;
            } else if (c == '}') {
                buffer.replace(0, pos + 1, "");
                return "]}";
            }

        default:
            throw new IllegalStateException();
        }
        pos++;
    }

    return null;
}

From source file:com.indoqa.lang.util.StringUtils.java

public static String sanitzeHtmlId(String id) {
    if (org.apache.commons.lang3.StringUtils.isBlank(id)) {
        return "";
    }//www. j a  v a  2  s  .  c om

    StringBuilder stringBuilder = new StringBuilder(id);

    for (int i = 0; i < stringBuilder.length(); i++) {
        char character = stringBuilder.charAt(i);

        if (Arrays.binarySearch(ILLEGAL_HTML_ID_CHARACTERS, character) >= 0) {
            stringBuilder.setCharAt(i, '_');
        }
    }

    return stringBuilder.toString();
}

From source file:gov.jgi.meta.sequence.SequenceString.java

private static byte[] pack(String sequenceToPack) {
    int numberOfBases = sequenceToPack.length();
    int numberOfFullBytes = numberOfBases / 3;
    int overflow = (numberOfBases % 3 == 0 ? 0 : 1);
    int numberOfBytes = numberOfFullBytes + overflow;

    byte[] bytes = new byte[numberOfBytes];

    int i = 0;/* www.j a va  2s. c  o  m*/

    try {
        for (i = 0; i < numberOfFullBytes; i++) {

            StringBuilder subseq = new StringBuilder(sequenceToPack.substring(i * 3, i * 3 + 3));
            for (int si = 0; si < 3; si++) {
                char sichar = subseq.charAt(si);
                if (sichar != 'a' && sichar != 't' && sichar != 'g' && sichar != 'c' && sichar != 'n') {
                    subseq.setCharAt(si, 'n');
                }
            }
            bytes[i] = hash.get(subseq.toString());

        }
    } catch (Exception e) {
        System.out.println("i = " + i);
    }

    if (overflow > 0) {
        StringBuilder subseq = new StringBuilder(sequenceToPack.substring(i * 3, i * 3 + numberOfBases % 3));
        for (int si = 0; si < numberOfBases % 3; si++) {
            char sichar = subseq.charAt(si);
            if (sichar != 'a' && sichar != 't' && sichar != 'g' && sichar != 'c' && sichar != 'n') {
                subseq.setCharAt(si, 'n');
            }
        }
        bytes[i++] = hash.get(subseq.toString());
    }

    // sanity check
    assert (i == numberOfBytes);

    return bytes;
}

From source file:Main.java

/**
 * Returns the ASCII characters up to but not including the next "\r\n", or
 * "\n".//from  ww w . j  a  va2 s . c  o m
 *
 * @throws java.io.EOFException if the stream is exhausted before the next newline
 * character.
 */
public static String readAsciiLine(InputStream in) throws IOException {
    // TODO: support UTF-8 here instead
    StringBuilder result = new StringBuilder(80);
    while (true) {
        int c = in.read();
        if (c == -1) {
            throw new EOFException();
        } else if (c == '\n') {
            break;
        }

        result.append((char) c);
    }
    int length = result.length();
    if (length > 0 && result.charAt(length - 1) == '\r') {
        result.setLength(length - 1);
    }
    return result.toString();
}

From source file:com.symphony.jirabot.clients.QuandlClient.java

private static String mapToQueryString(HashMap<String, Object> parameters) {
    if (parameters == null || parameters.size() == 0) {
        return "";
    }/*from  ww  w .jav a2  s . c  o m*/

    StringBuilder queryString = new StringBuilder("&");
    for (String key : parameters.keySet()) {
        queryString.append(urlEncodeUTF8(key));
        queryString.append("=");
        queryString.append(urlEncodeUTF8(parameters.get(key).toString()));
        queryString.append("&");
    }

    if (queryString.charAt(queryString.length() - 1) == '&') {
        queryString = queryString.deleteCharAt(queryString.length() - 1);
    }

    return queryString.toString();
}

From source file:Main.java

/**
 * Removes dot segments from the path of a URI.
 *
 * @param uri A {@link StringBuilder} containing the URI.
 * @param offset The index of the start of the path in {@code uri}.
 * @param limit The limit (exclusive) of the path in {@code uri}.
 *///from ww  w.ja v a  2s . co  m
private static String removeDotSegments(StringBuilder uri, int offset, int limit) {
    if (offset >= limit) {
        // Nothing to do.
        return uri.toString();
    }
    if (uri.charAt(offset) == '/') {
        // If the path starts with a /, always retain it.
        offset++;
    }
    // The first character of the current path segment.
    int segmentStart = offset;
    int i = offset;
    while (i <= limit) {
        int nextSegmentStart = -1;
        if (i == limit) {
            nextSegmentStart = i;
        } else if (uri.charAt(i) == '/') {
            nextSegmentStart = i + 1;
        } else {
            i++;
            continue;
        }
        // We've encountered the end of a segment or the end of the path. If the final segment was
        // "." or "..", remove the appropriate segments of the path.
        if (i == segmentStart + 1 && uri.charAt(segmentStart) == '.') {
            // Given "abc/def/./ghi", remove "./" to get "abc/def/ghi".
            uri.delete(segmentStart, nextSegmentStart);
            limit -= nextSegmentStart - segmentStart;
            i = segmentStart;
        } else if (i == segmentStart + 2 && uri.charAt(segmentStart) == '.'
                && uri.charAt(segmentStart + 1) == '.') {
            // Given "abc/def/../ghi", remove "def/../" to get "abc/ghi".
            int prevSegmentStart = uri.lastIndexOf("/", segmentStart - 2) + 1;
            int removeFrom = prevSegmentStart > offset ? prevSegmentStart : offset;
            uri.delete(removeFrom, nextSegmentStart);
            limit -= nextSegmentStart - removeFrom;
            segmentStart = prevSegmentStart;
            i = prevSegmentStart;
        } else {
            i++;
            segmentStart = i;
        }
    }
    return uri.toString();
}

From source file:com.indoqa.lang.util.URLStringUtils.java

public static StringBuilder cleanMultipleChar(StringBuilder text, char doubleCh) {
    if (text == null) {
        return null;
    }/*from ww w .j a  v  a 2 s .c o m*/

    StringBuilder result = new StringBuilder();

    char previousChar = 0;
    for (int i = 0; i < text.length(); i++) {
        char currentChar = text.charAt(i);

        if (previousChar == doubleCh && previousChar == currentChar) {
            previousChar = currentChar;
            continue;
        }

        previousChar = currentChar;
        result.append(currentChar);
    }

    return result;
}

From source file:org.apache.karaf.tooling.RunMojo.java

/**
 * Return a path for an artifact:/*ww w  .j  av a 2s .c  o  m*/
 * - if the input is already a path (doesn't contain ':'), the same path is returned.
 * - if the input is a Maven URL, the input is converted to a default repository location path, type and classifier
 *   are optional.
 *
 * @param name artifact data
 * @return path as supplied or a default Maven repository path
 */
private static String fromMaven(String name) {
    Matcher m = mvnPattern.matcher(name);
    if (!m.matches()) {
        return name;
    }

    StringBuilder b = new StringBuilder();
    b.append(m.group(1));
    for (int i = 0; i < b.length(); i++) {
        if (b.charAt(i) == '.') {
            b.setCharAt(i, '/');
        }
    }
    b.append("/"); // groupId
    String artifactId = m.group(2);
    String version = m.group(3);
    String extension = m.group(5);
    String classifier = m.group(7);
    b.append(artifactId).append("/"); // artifactId
    b.append(version).append("/"); // version
    b.append(artifactId).append("-").append(version);
    if (present(classifier)) {
        b.append("-").append(classifier);
    }
    if (present(classifier)) {
        b.append(".").append(extension);
    } else {
        b.append(".jar");
    }
    return b.toString();
}

From source file:com.yahoo.sql4d.sql4ddriver.Util.java

public static String capitalize(String word) {
    StringBuilder buff = new StringBuilder(word);
    if (word.charAt(0) != '_') {
        buff.setCharAt(0, Character.toUpperCase(word.charAt(0)));
    }//from  w  ww. j  av a  2s.co  m
    for (int i = 1; i < buff.length(); i++) {
        if (buff.charAt(i - 1) == '_') {
            buff.setCharAt(i, Character.toUpperCase(word.charAt(i)));
        }
    }
    return buff.toString().replace("_", "");
}

From source file:com.fluidops.iwb.ui.configuration.WikiWidgetConfigurationForm.java

/**
 * Searches for the end of a widget configuration in a string having a
 * string and the start index of the widget configuration. Returns -1 if
 * nothing found/*w  ww.  j a v a  2s.c om*/
 * 
 * @param searchString
 * @param widgetStart
 */
private static int findWidgetConfigurationEnd(String searchString, int widgetStart) {

    if (widgetStart < 0 || widgetStart > searchString.length())
        return -1;

    StringBuilder sb = new StringBuilder(searchString.substring(widgetStart));

    int numberOfBrackets = 0;

    for (int i = 0; i < sb.length(); i++) {
        if (sb.charAt(i) == '{') {
            numberOfBrackets++;
        } else if (sb.charAt(i) == '}') {
            numberOfBrackets--;
        }

        if (numberOfBrackets == 0) {
            return widgetStart + i + 1;
        }
    }
    return -1;
}