Example usage for java.lang StringBuilder deleteCharAt

List of usage examples for java.lang StringBuilder deleteCharAt

Introduction

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

Prototype

@Override
public StringBuilder deleteCharAt(int index) 

Source Link

Usage

From source file:de.micromata.genome.gwiki.page.gspt.ExtendedTemplate.java

/**
 * Compress ws string./*from   ww  w . j av a 2s.  com*/
 *
 * @param text the text
 */
private void compressWsString(StringBuilder text) {
    char lastChar = 0;
    for (int i = 0; i < text.length(); ++i) {
        char cc = text.charAt(i);
        if (lastChar == ' ' && cc == ' ') {
            text.deleteCharAt(i);
            --i;
            continue;
        }
        lastChar = cc;
    }
}

From source file:com.htmlhifive.tools.codeassist.core.proposal.build.ObjectLiteralCodeBuilder.java

@Override
protected void buildEnd(StringBuilder sb, StringBuilder part, int tempInsertPosition) {

    // ?//from  w ww . j a v  a  2  s  .  co m
    boolean delflg = true;
    int deletePosition = 0;
    int insertPosition = tempInsertPosition;
    for (int currentPosition = insertPosition - 1; currentPosition > 0; currentPosition--) {
        if (sb.charAt(currentPosition) == ',') {
            deletePosition = currentPosition;
            break;
        }
        if (CharUtils.isAsciiPrintable(sb.charAt(currentPosition))) {
            delflg = false;
            break;
        }
    }
    // while (sb.charAt(currentPosition) != ',') {
    // currentPosition--;
    // System.out.println(sb.charAt(currentPosition));
    // }
    if (delflg) {
        insertPosition--;
        sb.deleteCharAt(deletePosition);
    }
    super.buildEnd(sb, part, insertPosition);
}

From source file:com.heliosapm.tsdblite.metric.Metric.java

public ObjectName toHostObjectName() {
    final StringBuilder b = new StringBuilder("metrics.");
    TreeMap<String, String> tgs = new TreeMap<String, String>(tags);
    String h = tgs.remove("host");
    String a = tgs.remove("app");
    final String host = h == null ? "unknownhost" : h;
    final int segIndex = metricName.indexOf('.');
    final String seg = segIndex == -1 ? metricName : metricName.substring(0, segIndex);
    b.append(host).append(".").append(seg).append(":");
    if (segIndex != -1) {
        tgs.put("app", metricName.substring(segIndex + 1));
    }/* www.ja  v a 2s .c o  m*/
    for (Map.Entry<String, String> entry : tgs.entrySet()) {
        b.append(entry.getKey()).append("=").append(entry.getValue()).append(",");
    }
    b.deleteCharAt(b.length() - 1);
    return JMXHelper.objectName(b);
}

From source file:com.qmetry.qaf.automation.data.BaseDataBean.java

@Override
public String toString() {
    StringBuilder sb = new StringBuilder("[");
    Field[] flds = getFields();/*w  ww. j  a v  a 2  s .  c o  m*/
    for (Field fld : flds) {
        try {
            fld.setAccessible(true);
            if ((fld.get(this) != null)) {
                sb.append(fld.getName() + " : " + fld.get(this).toString());
                sb.append(",");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    sb.deleteCharAt(sb.length() - 1);
    if (sb.length() > 0) {
        sb.append("]");
    }

    return sb.toString();
}

From source file:org.openspaces.rest.space.ReplicationRESTController.java

private String addressesToNat(Map<String, List<String>> gwaddresses) {
    StringBuilder sb = new StringBuilder("[");
    for (Map.Entry<String, List<String>> entry : gwaddresses.entrySet()) {
        sb.append(entry.getValue().get(1)).append(":").append(entry.getValue().get(0)).append(",");
    }/*w ww.j av a2  s.c o m*/
    sb.deleteCharAt(sb.length() - 1);
    sb.append("]");
    log.info("addressesToNat returning:" + sb.toString());
    return sb.toString();
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlTextArea.java

private String readValue() {
    final StringBuilder buffer = new StringBuilder();
    for (final DomNode node : getChildren()) {
        if (node instanceof DomText) {
            buffer.append(((DomText) node).getData());
        }//from www . j ava  2  s.  com
    }
    // if content starts with new line, it is ignored (=> for the parser?)
    if (buffer.length() != 0 && buffer.charAt(0) == '\n') {
        buffer.deleteCharAt(0);
    }
    return buffer.toString();
}

From source file:de.iteratec.iteraplan.businesslogic.service.SearchServiceImpl.java

private String modifyQueryString(String queryString) {
    StringBuilder modQueryString = new StringBuilder(queryString);
    // if whitespace included, don't modify
    if (!queryString.contains(" ")) {

        // if the String contains quotes, remove them if they are at the beginning or end
        if (queryString.contains("\"")) {
            if (queryString.endsWith("\"")) {
                modQueryString.deleteCharAt(queryString.length() - 1);
            }/* ww  w .  j a v  a  2  s .  c o  m*/
            if (queryString.charAt(0) == '"') {
                modQueryString.deleteCharAt(0);
            }
            // no further modification, return the string
            return modQueryString.toString();
        }

        // if the String contains no "*" surround it with "*"
        if (!queryString.contains("*")) {
            modQueryString.insert(0, '*');
            modQueryString.append('*');
        }
    }
    return modQueryString.toString();
}

From source file:edu.psu.iam.cpr.core.util.Utility.java

/**
 * <p>Convert IPv6 address into RFC 5952 form.
 * E.g. 2001:db8:0:1:0:0:0:1 -> 2001:db8:0:1::1</p>
 * <p/>/*  w  w w  .  ja v  a 2 s.  com*/
 * <p>Method is null safe, and if IPv4 address or host name is passed to the
 * method it is returned wihout any processing.</p>
 * <p/>
 * <p>Method also supports IPv4 in IPv6 (e.g. 0:0:0:0:0:ffff:192.0.2.1 ->
 * ::ffff:192.0.2.1), and zone ID (e.g. fe80:0:0:0:f0f0:c0c0:1919:1234%4
 * -> fe80::f0f0:c0c0:1919:1234%4).</p>
 *
 * @param ipv6Address String representing valid IPv6 address.
 * @return String representing IPv6 in canonical form.
 * @throws IllegalArgumentException if IPv6 format is unacceptable.
 */
public static String canonicalizeAddress(final String ipv6Address) throws IllegalArgumentException {

    if (ipv6Address == null) {
        return null;
    }

    // Definitely not an IPv6, return untouched input.
    if (!mayBeIPv6Address(ipv6Address)) {
        return ipv6Address;
    }

    // Length without zone ID (%zone) or IPv4 address
    int ipv6AddressLength = ipv6Address.length();
    if (ipv6Address.contains(":") && ipv6Address.contains(".")) {
        // IPv4 in IPv6
        // e.g. 0:0:0:0:0:FFFF:127.0.0.1
        final int lastColonPos = ipv6Address.lastIndexOf(':');
        final int lastColonsPos = ipv6Address.lastIndexOf("::");
        if (lastColonsPos >= 0 && lastColonPos == lastColonsPos + 1) {
            /*
             *  IPv6 part ends with two consecutive colons,
             *  last colon is part of IPv6 format.
             *  e.g. ::127.0.0.1
             */
            ipv6AddressLength = lastColonPos + 1;
        } else {
            /*
             *  IPv6 part ends with only one colon,
             *  last colon is not part of IPv6 format.
             *  e.g. ::FFFF:127.0.0.1
             */
            ipv6AddressLength = lastColonPos;
        }
    } else if (ipv6Address.contains(":") && ipv6Address.contains("%")) {
        // Zone ID
        // e.g. fe80:0:0:0:f0f0:c0c0:1919:1234%4
        ipv6AddressLength = ipv6Address.lastIndexOf('%');
    }

    final StringBuilder result = new StringBuilder();
    final char[][] groups = new char[MAX_NUMBER_OF_GROUPS][MAX_GROUP_LENGTH];
    int groupCounter = 0;
    int charInGroupCounter = 0;

    // Index of the current zeroGroup, -1 means not found.
    int zeroGroupIndex = -1;
    int zeroGroupLength = 0;

    // maximum length zero group, if there is more then one, then first one
    int maxZeroGroupIndex = -1;
    int maxZeroGroupLength = 0;

    boolean isZero = true;
    boolean groupStart = true;

    /*
     *  Two consecutive colons, initial expansion.
     *  e.g. 2001:db8:0:0:1::1 -> 2001:db8:0:0:1:0:0:1
     */

    final StringBuilder expanded = new StringBuilder(ipv6Address);
    final int colonsPos = ipv6Address.indexOf("::");
    int length = ipv6AddressLength;
    int change = 0;

    if (colonsPos >= 0 && colonsPos < ipv6AddressLength - 2) {
        int colonCounter = 0;
        for (int i = 0; i < ipv6AddressLength; i++) {
            if (ipv6Address.charAt(i) == ':') {
                colonCounter++;
            }
        }

        if (colonsPos == 0) {
            expanded.insert(0, "0");
            change = change + 1;
        }

        for (int i = 0; i < MAX_NUMBER_OF_GROUPS - colonCounter; i++) {
            expanded.insert(colonsPos + 1, "0:");
            change = change + 2;
        }

        if (colonsPos == ipv6AddressLength - 2) {
            expanded.setCharAt(colonsPos + change + 1, '0');
        } else {
            expanded.deleteCharAt(colonsPos + change + 1);
            change = change - 1;
        }
        length = length + change;
    }

    // Processing one char at the time
    for (int charCounter = 0; charCounter < length; charCounter++) {
        char c = expanded.charAt(charCounter);
        if (c >= 'A' && c <= 'F') {
            c = (char) (c + 32);
        }
        if (c != ':') {
            groups[groupCounter][charInGroupCounter] = c;
            if (!(groupStart && c == '0')) {
                ++charInGroupCounter;
                groupStart = false;
            }
            if (c != '0') {
                isZero = false;
            }
        }
        if (c == ':' || charCounter == length - 1) {
            // We reached end of current group
            if (isZero) {
                ++zeroGroupLength;
                if (zeroGroupIndex == -1) {
                    zeroGroupIndex = groupCounter;
                }
            }

            if (!isZero || charCounter == length - 1) {
                // We reached end of zero group
                if (zeroGroupLength > maxZeroGroupLength) {
                    maxZeroGroupLength = zeroGroupLength;
                    maxZeroGroupIndex = zeroGroupIndex;
                }
                zeroGroupLength = 0;
                zeroGroupIndex = -1;
            }
            ++groupCounter;
            charInGroupCounter = 0;
            isZero = true;
            groupStart = true;
        }
    }

    final int numberOfGroups = groupCounter;

    // Output results
    for (groupCounter = 0; groupCounter < numberOfGroups; groupCounter++) {
        if (maxZeroGroupLength <= 1 || groupCounter < maxZeroGroupIndex
                || groupCounter >= maxZeroGroupIndex + maxZeroGroupLength) {
            for (int j = 0; j < MAX_GROUP_LENGTH; j++) {
                if (groups[groupCounter][j] != 0) {
                    result.append(groups[groupCounter][j]);
                }
            }
            if (groupCounter < numberOfGroups - 1
                    && (groupCounter != maxZeroGroupIndex - 1 || maxZeroGroupLength <= 1)) {
                result.append(':');
            }
        } else if (groupCounter == maxZeroGroupIndex) {
            result.append("::");
        }
    }

    // Solve problem with three colons in IPv4 in IPv6 format
    // e.g. 0:0:0:0:0:0:127.0.0.1 -> :::127.0.0.1 -> ::127.0.0.1
    final int resultLength = result.length();
    if (result.charAt(resultLength - 1) == ':' && ipv6AddressLength < ipv6Address.length()
            && ipv6Address.charAt(ipv6AddressLength) == ':') {
        result.delete(resultLength - 1, resultLength);
    }

    /*
     * Append IPv4 from IPv4-in-IPv6 format or Zone ID
     */
    for (int i = ipv6AddressLength; i < ipv6Address.length(); i++) {
        result.append(ipv6Address.charAt(i));
    }

    return result.toString();
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlTextArea.java

private String readValueIE() {
    final StringBuilder buffer = new StringBuilder();
    for (final DomNode node : getDescendants()) {
        if (node instanceof DomText) {
            buffer.append(((DomText) node).getData());
        }//from w  w w. jav  a 2 s. com
    }
    // if content starts with new line, it is ignored (=> for the parser?)
    if (buffer.length() != 0 && buffer.charAt(0) == '\n') {
        buffer.deleteCharAt(0);
    }
    return buffer.toString();
}

From source file:com.moviejukebox.tools.WebBrowser.java

private String createCookieHeader(URLConnection cnx) {
    String host = cnx.getURL().getHost();
    StringBuilder cookiesHeader = new StringBuilder();
    for (Map.Entry<String, Map<String, String>> domainCookies : cookies.entrySet()) {
        if (host.endsWith(domainCookies.getKey())) {
            for (Map.Entry<String, String> cookie : domainCookies.getValue().entrySet()) {
                cookiesHeader.append(cookie.getKey());
                cookiesHeader.append("=");
                cookiesHeader.append(cookie.getValue());
                cookiesHeader.append(";");
            }// w  ww.jav  a  2s  .  c  o m
        }
    }
    if (cookiesHeader.length() > 0) {
        // remove last ; char
        cookiesHeader.deleteCharAt(cookiesHeader.length() - 1);
    }
    return cookiesHeader.toString();
}