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:net.jofm.format.Format.java

private String trimRightPad(String data) {
    if (data == null || data.length() == 0) {
        return data;
    }/*from   w  w w.  j  av  a  2s.  c o  m*/
    StringBuilder builder = new StringBuilder(data);
    while (builder.length() > 0 && builder.charAt(builder.length() - 1) == padWith) {
        builder.deleteCharAt(builder.length() - 1);
    }
    return builder.toString();
}

From source file:com.aiblockchain.api.StringUtils.java

/**
 * Ensures that the given string builder has two newline characters at the end of its buffer.
 *
 * @param stringBuilder the given string builder
 *///from www. j a  v a2  s .  c  om
public static void ensureTwoNewLines(final StringBuilder stringBuilder) {
    //Preconditions
    assert stringBuilder != null : "stringBuilder must not be null";

    final int length = stringBuilder.length();
    if (length == 0) {
        stringBuilder.append('\n');
        stringBuilder.append('\n');
        return;
    }
    final char ch1 = stringBuilder.charAt(length - 1);
    if (length == 1) {
        stringBuilder.append('\n');
        if (ch1 == '\n') {
            return;
        } else {
            stringBuilder.append('\n');
            return;
        }
    }
    final char ch2 = stringBuilder.charAt(length - 2);
    if (ch1 == '\n') {
        if (ch2 == '\n') {
            return;
        } else {
            stringBuilder.append('\n');
            return;
        }
    } else {
        stringBuilder.append('\n');
        stringBuilder.append('\n');
        return;
    }
}

From source file:com.untangle.app.web_filter.WebFilterDecisionEngine.java

/**
 * Encode a URL/*from www .jav a  2  s.  co m*/
 * 
 * @param domain
 *        The domain
 * @param uri
 *        The URI
 * @return The encoded URL
 */
private static String encodeUrl(String domain, String uri) {
    // Remote trailing dots from domain
    Matcher matcher = trailingDotsPattern.matcher(domain);
    domain = matcher.replaceAll("");

    String url = domain + uri;

    // Remote trailing dots or slashes from URL
    matcher = trailingDotsSlashesPattern.matcher(url);
    url = matcher.replaceAll("");

    StringBuilder qBuilder = new StringBuilder(url);

    int i;
    int lastDot = 0;

    for (i = 0; i < qBuilder.length(); i++) {

        int numCharsAfterThisOne = (qBuilder.length() - i) - 1;

        // Must insert a null escape to divide long spans
        if (i > lastDot + 59) {
            qBuilder.insert(i, "_-0.");
            lastDot = i + 3;
            i += 4;
        }

        if (qBuilder.charAt(i) == '.') {
            lastDot = i;
        }
        // Take care of the rare, but possible case of _- being in the string
        else if (qBuilder.charAt(i) == '_' && numCharsAfterThisOne >= 1 && qBuilder.charAt(i + 1) == '-') {
            qBuilder.replace(i, i + 2, "_-_-");
            i += 4;
        }
        // Convert / to rfc compliant characters _-.
        else if (qBuilder.charAt(i) == '/') {
            qBuilder.replace(i, i + 1, "_-.");
            lastDot = i + 2;
            i += 3;
        }
        // Convert any dots next to each other
        else if (qBuilder.charAt(i) == '.' && numCharsAfterThisOne >= 1 && qBuilder.charAt(i + 1) == '.') {
            qBuilder.replace(i, i + 2, "._-2e");
            i += 5;
        }
        // Convert any dots at the end. (these should have already been stripped but the zvelo implementation has this here)
        else if (qBuilder.charAt(i) == '.' && numCharsAfterThisOne == 0) {
            qBuilder.replace(i, i + 1, "_-2e");
            i += 4;
        }
        // Convert : to _--
        else if (qBuilder.charAt(i) == ':') {
            qBuilder.replace(i, i + 1, "_--");
            i += 3;
        }
        // Drop everything after ? or #
        else if (qBuilder.charAt(i) == '?' || qBuilder.charAt(i) == '#') {
            qBuilder.delete(i, qBuilder.length());
            break;
        }
        // Convert %HEXHEX to encoded form
        else if (qBuilder.charAt(i) == '%' && numCharsAfterThisOne >= 2 && _isHex(qBuilder.charAt(i + 1))
                && _isHex(qBuilder.charAt(i + 2))) {
            //String hexString = new String(new char[] {qBuilder.charAt(i+1), qBuilder.charAt(i+2)});
            //char c = (char)( Integer.parseInt( hexString , 16) );
            //System.out.println("HEX: \"" + hexString +"\" -> \"" + Character.toString(c) + "\"");
            qBuilder.replace(i, i + 1, "_-");
            i += 4; // 2 for length of replacement + 2 for the hex characters
        }
        // Convert % charaters to encoded form
        else if (qBuilder.charAt(i) == '%') {
            qBuilder.replace(i, i + 1, "_-25");
            i += 4;
        }
        // Convert any remaining non-RFC characters.
        else if (!_isRfc(qBuilder.charAt(i))) {
            String replaceStr = String.format("_-%02X", ((byte) qBuilder.charAt(i)));
            qBuilder.replace(i, i + 1, replaceStr);
            i += 4;
        }
    }

    return qBuilder.toString();
}

From source file:com.cheusov.Jrep.java

private static String getOutputString(String line, JrepMatchResult match) {
    if (opt_O == null)
        return line.substring(match.start(), match.end());

    StringBuilder b = new StringBuilder();
    int len = opt_O.length();
    for (int i = 0; i < len; ++i) {
        char c = opt_O.charAt(i);
        if (c != '$') {
            b.append(c);/*from www . ja v  a 2  s  . co m*/
        } else {
            if (i + 1 == len)
                throw new IllegalArgumentException("Unexpected `$` in -O argument: `" + opt_O + "`");
            char nc = opt_O.charAt(i + 1);
            if (nc == '$')
                b.append('$');
            else if (nc == '{') {
                StringBuilder[] ld;
                i += 1;
                ld = extractCurlyBraces(opt_O, i + 1);
                StringBuilder l = ld[0];
                StringBuilder d = ld[1];
                int sumlen = d.length() + l.length();
                i += sumlen; // +1 due to '}'
                int groupNum = Integer.valueOf(d.toString());

                String group = match.group(groupNum);
                if (group == null)
                    group = "";

                for (int j = 0; j < l.length(); ++j) {
                    char lc = l.charAt(j);
                    switch (lc) {
                    case 'n':
                        group = group.replaceAll("\\\\", "\\\\\\\\").replaceAll("\n", "\\\\n");
                        break;
                    case 'N':
                        group = group.replaceAll("\n", " ");
                        break;
                    case 's':
                        group = group.replaceAll("\\s+", " ");
                        break;
                    case 't':
                        group = group.trim();
                        break;
                    }
                }
                b.append(group);
            } else if (nc >= '0' && nc <= '9') {
                String group = match.group(nc - '0');
                if (group == null)
                    group = "";

                b.append(group);
            } else {
                throw new IllegalArgumentException("Illegal `$" + nc + "` in -O argument: `" + opt_O + "`");
            }

            ++i;
        }
    }

    return b.toString();
}

From source file:com.flexive.shared.XPathElement.java

/**
 * Build an XPath from the given elements
 *
 * @param leadingSlash prepend a leading slash character?
 * @param elements     elements that build the XPath
 * @return XPath/*from www  .j a va  2 s.  co m*/
 */
public static String buildXPath(boolean leadingSlash, String... elements) {
    StringBuilder XPath = new StringBuilder(100);
    for (String element : elements) {
        if (element == null)
            continue;
        XPath.append('/');
        if (element.length() > 0 && element.charAt(0) == '/')
            XPath.append(element.substring(1));
        else
            XPath.append(element);
        if (XPath.length() > 1 && XPath.charAt(XPath.length() - 1) == '/')
            XPath.deleteCharAt(XPath.length() - 1);
    }
    if (XPath.length() > 0) {
        if (XPath.charAt(0) == '/' && !leadingSlash)
            XPath.deleteCharAt(0);
    } else if (XPath.length() == 0 && leadingSlash)
        XPath.append('/');
    return xpToUpperCase(doubleSlashPattern.matcher(XPath).replaceAll("/"));
}

From source file:net.jforum.util.JForumConfig.java

private void normalizeTemplateDirectory() {
    StringBuilder sb = new StringBuilder(getValue(ConfigKeys.TEMPLATE_DIRECTORY));

    if (sb.charAt(0) != '/') {
        sb.insert(0, '/');
    }/*from w  ww.  j  a  v a 2 s  .c o m*/

    if (sb.charAt(sb.length() - 1) != '/') {
        sb.append('/');
    }

    setProperty(ConfigKeys.TEMPLATE_DIRECTORY, sb.toString());
}

From source file:com.rabbitframework.commons.utils.StringUtils.java

/**
 * _,-,@,$,#,' ',/,&?// w ww  . j  a  v a 2  s .  co  m
 * <p/>
 * :hello_world ?helloWorld
 *
 * @param inputString
 * @param firstCharacterUppercase
 *            ??
 * @return
 */
public static String toCamelCase(String inputString, boolean firstCharacterUppercase) {
    StringBuilder sb = new StringBuilder();
    boolean nextUpperCase = false;
    for (int i = 0; i < inputString.length(); i++) {
        char c = inputString.charAt(i);
        switch (c) {
        case '_':
        case '-':
        case '@':
        case '$':
        case '#':
        case ' ':
        case '/':
        case '&':
            if (sb.length() > 0) {
                nextUpperCase = true;
            }
            break;
        default:
            if (nextUpperCase) {
                sb.append(Character.toUpperCase(c));
                nextUpperCase = false;
            } else {
                sb.append(Character.toLowerCase(c));
            }
            break;
        }
    }

    if (firstCharacterUppercase) {
        sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
    }

    return sb.toString();
}

From source file:org.glite.lb.NotifParser.java

/**
 * this method creates an XML document out of a provided String
 * note that the string has to be a valid escaped XML document representation,
 * otherwise the process will fail//ww  w.j  a  v a2s.c  om
 *
 * @param body a String containing an XML document
 * @return an XML document in the Document format
 * @throws javax.xml.parsers.ParserConfigurationException
 * @throws org.xml.sax.SAXException
 * @throws java.io.IOException
 */
private Document createXML(String body) throws ParserConfigurationException, SAXException, IOException {
    String removed = body.substring(0, body.length() - 1);
    String parsed = StringEscapeUtils.unescapeXml(removed);
    //this code removes hexadecimal references from the string (couldn't find
    //a suitable parser for this)
    StringBuilder build = new StringBuilder(parsed);
    int j = build.indexOf("%");
    while (j > 0) {
        if (build.charAt(j - 1) == '>') {
            build.delete(j, j + 3);
            if (build.charAt(j) == '<') {
                j = build.indexOf("%");
            }
        } else {
            j = build.indexOf("%", j + 1);
        }
    }
    parsed = build.toString();
    //ends here
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    return factory.newDocumentBuilder().parse(new InputSource(new StringReader(parsed)));
}

From source file:net.sourceforge.fenixedu.domain.candidacyProcess.erasmus.TypeOfProgrammeList.java

private String convertToString(Set<TypeOfProgramme> types) {
    final StringBuilder result = new StringBuilder();

    for (TypeOfProgramme each : types) {
        result.append(each.name()).append(",");
    }//w  w w  .j a v  a 2s  .c o  m

    if (result.length() > 0 && result.charAt(result.length() - 1) == ',') {
        result.deleteCharAt(result.length() - 1);
    }

    return result.toString();
}

From source file:net.sourceforge.fenixedu.domain.phd.PhdElementsList.java

private String convertToString(Set<T> types) {
    final StringBuilder result = new StringBuilder();

    for (T each : types) {
        result.append(convertElementToString(each)).append(",");
    }/*from   ww  w  .j  a v a 2s  .  c o m*/

    if (result.length() > 0 && result.charAt(result.length() - 1) == ',') {
        result.deleteCharAt(result.length() - 1);
    }

    return result.toString();
}