Example usage for java.lang StringBuilder replace

List of usage examples for java.lang StringBuilder replace

Introduction

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

Prototype

@Override
public StringBuilder replace(int start, int end, String str) 

Source Link

Usage

From source file:org.kuali.maven.plugins.graph.dot.html.HtmlUtils.java

public String toHtml(Map<String, ?> attributes) {
    if (attributes.size() == 0) {
        return "";
    }//from www  .ja va 2 s.  co m
    StringBuilder sb = new StringBuilder();
    for (String key : attributes.keySet()) {
        String value = attributes.get(key).toString();
        String name = getTranslatedAttributeName(key);
        sb.append(name + "=" + quote(value) + " ");
    }
    sb.replace(sb.lastIndexOf(" "), sb.length(), "");
    return " " + sb.toString();
}

From source file:StringUtils.java

/**
 * Reformats a string where lines that are longer than <tt>width</tt>
 * are split apart at the earliest wordbreak or at maxLength, whichever is
 * sooner. If the width specified is less than 5 or greater than the input
 * Strings length the string will be returned as is.
 * <p/>/*from  ww  w  .j  a v a 2s  .  co  m*/
 * Please note that this method can be lossy - trailing spaces on wrapped
 * lines may be trimmed.
 *
 * @param input the String to reformat.
 * @param width the maximum length of any one line.
 * @return a new String with reformatted as needed.
 */
public static String wordWrap(String input, int width, Locale locale) {
    // protect ourselves
    if (input == null) {
        return "";
    } else if (width < 5) {
        return input;
    } else if (width >= input.length()) {
        return input;
    }

    StringBuilder buf = new StringBuilder(input);
    boolean endOfLine = false;
    int lineStart = 0;

    for (int i = 0; i < buf.length(); i++) {
        if (buf.charAt(i) == '\n') {
            lineStart = i + 1;
            endOfLine = true;
        }

        // handle splitting at width character
        if (i > lineStart + width - 1) {
            if (!endOfLine) {
                int limit = i - lineStart - 1;
                BreakIterator breaks = BreakIterator.getLineInstance(locale);
                breaks.setText(buf.substring(lineStart, i));
                int end = breaks.last();

                // if the last character in the search string isn't a space,
                // we can't split on it (looks bad). Search for a previous
                // break character
                if (end == limit + 1) {
                    if (!Character.isWhitespace(buf.charAt(lineStart + end))) {
                        end = breaks.preceding(end - 1);
                    }
                }

                // if the last character is a space, replace it with a \n
                if (end != BreakIterator.DONE && end == limit + 1) {
                    buf.replace(lineStart + end, lineStart + end + 1, "\n");
                    lineStart = lineStart + end;
                }
                // otherwise, just insert a \n
                else if (end != BreakIterator.DONE && end != 0) {
                    buf.insert(lineStart + end, '\n');
                    lineStart = lineStart + end + 1;
                } else {
                    buf.insert(i, '\n');
                    lineStart = i + 1;
                }
            } else {
                buf.insert(i, '\n');
                lineStart = i + 1;
                endOfLine = false;
            }
        }
    }

    return buf.toString();
}

From source file:net.duckling.ddl.service.resource.dao.TagItemDAOImpl.java

@Override
public void deleteTagItem(List<Integer> rids, Integer tagId) {
    if (null == rids || rids.size() <= 0) {
        return;/*from   w w  w  .j a  v a 2  s.  c om*/
    }
    String sql = "delete from a1_tag_item where tgid=? and rid in(";
    StringBuilder sb = new StringBuilder();
    for (Integer rid : rids) {
        sb.append(rid + ",");
    }
    sb.replace(sb.lastIndexOf(","), sb.length(), ")");
    sql += sb.toString();
    this.getJdbcTemplate().update(sql, new Object[] { tagId });
}

From source file:org.talend.components.salesforce.soql.SoqlQueryBuilder.java

/**
 * Finds all custom values in <code>fieldName</code> and replace "_" between them with ".",<br/>
 * if the last field is not a custom, split all "_" with ".".<br/>
 * Example:<br/>//from   w  w  w. j  a  va 2 s. c  o  m
 * <b>Schema</b> contains such fields:
 *      <ol>
 *          <li><code>custom_module__r_custom_table__c_custom_name__c</code></li>
 *          <li><code>custom_module__r_custom_table__c_Person_Name</code></li>
 *      </ol>
 * <b> Result </b> must be this one:
 *      <ol>
 *          <li><code>custom_module__r.custom_table__c.custom_name__c</code></li>
 *          <li><code>custom_module__r.custom_table__c.Person.Name</code></li>
 *      </ol>
 *
 * @param fieldName - field name in {@link org.apache.avro.Schema}.
 * @return replaced input fieldName with "." instead of "_" where needed.
 */
private String splitParentCustomField(String fieldName) {
    StringBuilder sb = new StringBuilder(fieldName);
    Matcher matcher = PATTERN.matcher(fieldName);
    int lastPostition = 0;
    matcher.find();
    do {
        sb.replace(matcher.end(1) - 1, matcher.end(1), DOT);
        lastPostition = matcher.end(1);
    } while (matcher.find());

    if (!fieldName.endsWith(CUSTOM_FIELD_SUFFIX) && !fieldName.endsWith(RELATION_ENTITY_SUFFIX)) {
        String nonCustomRelationField = fieldName.substring(lastPostition).replaceAll(UNDERSCORE, DOT);
        sb.replace(lastPostition, sb.length(), nonCustomRelationField);
    }
    return sb.toString();
}

From source file:com.buaa.cfs.fs.permission.FsPermission.java

@Override
public String toString() {
    String str = useraction.SYMBOL + groupaction.SYMBOL + otheraction.SYMBOL;
    if (stickyBit) {
        StringBuilder str2 = new StringBuilder(str);
        str2.replace(str2.length() - 1, str2.length(), otheraction.implies(FsAction.EXECUTE) ? "t" : "T");
        str = str2.toString();/*from  w  w  w  .ja  va 2s. c  o  m*/
    }

    return str;
}

From source file:net.sf.maltcms.chromaui.normalization.spi.charts.PeakGroupRtBoxPlot.java

protected String getPeakName(IPeakGroupDescriptor pgd) {
    String rt = "mean rt: " + String.format("%.2f", pgd.getMeanApexTime()) + "+/-"
            + String.format("%.2f", pgd.getApexTimeStdDev()) + "; median rt: "
            + String.format("%.2f", pgd.getMedianApexTime()) + ": ";
    LinkedHashMap<String, Integer> names = new LinkedHashMap<>();
    if (!pgd.getDisplayName().equals(pgd.getName())) {
        return rt + pgd.getDisplayName();
    }/*from  ww w.  ja v a 2s  . c om*/
    for (IPeakAnnotationDescriptor ipad : pgd.getPeakAnnotationDescriptors()) {
        if (names.containsKey(ipad.getName())) {
            names.put(ipad.getName(), names.get(ipad.getName()) + 1);
        } else {
            names.put(ipad.getName(), 1);
        }
    }
    if (names.isEmpty()) {
        return rt + "<NA>";
    }
    if (names.size() > 1) {
        StringBuilder sb = new StringBuilder();
        for (String key : names.keySet()) {
            sb.append(key);
            sb.append(" (" + names.get(key) + ")");
            sb.append(" | ");
        }
        return rt + sb.replace(sb.length() - 1, sb.length() - 1, "").toString();
    } else {
        return rt + names.keySet().toArray(new String[0])[0];
    }
}

From source file:com.conwet.xjsp.session.Negotiator.java

/**
 * Consume available data and returns a new state when a transition is
 * reached./*from   w  ww  .  j a  va2 s.c  om*/
 *
 * TODO: violates the least-surprise principle
 *
 * @param buffer
 * @return State to transit to or null
 * @throws ConnectionException
 */
private NegotiationPhase consumeData(StringBuilder buffer) throws ConnectionException {

    Matcher m;
    switch (phase) {
    case start:
        m = START_PATTERN.matcher(buffer);
        if (m.find()) {
            buffer.replace(0, m.end(), "");
            return NegotiationPhase.header;
        }
        return null;

    case header:
        try {
            String header = JSONUtil.extractStanza(buffer);
            if (header == null) {
                return null;
            }
            JSONObject jsonHeader = (JSONObject) parser.parse(header);
            validateHeader(jsonHeader);
            extractFeaturePrefixes(jsonHeader);
            return NegotiationPhase.messages;
        } catch (ParseException ex) {
            throw new ConnectionException(ConnError.ProtocolSyntaxError, "Invalid header: " + ex.getMessage());
        }

    case messages:
        m = MESSAGE_PATTERN.matcher(buffer);
        if (m.find()) {
            buffer.replace(0, m.end(), "");
            return NegotiationPhase.finished;
        }
        return null;

    case finished:
        return null;

    default: // should never reach this point
        throw new IllegalStateException("Unexpected exception");
    }
}

From source file:net.sf.maltcms.chromaui.normalization.spi.charts.PeakGroupBoxPlot.java

protected String getPeakName(IPeakGroupDescriptor pgd) {
    String rt = "mean area: " + String.format("%.2f", pgd.getMeanArea(normalizer)) + "+/-"
            + String.format("%.2f", pgd.getAreaStdDev(normalizer)) + "; median area: "
            + String.format("%.2f", pgd.getMedianArea(normalizer)) + ": ";
    LinkedHashMap<String, Integer> names = new LinkedHashMap<>();
    if (!pgd.getDisplayName().equals(pgd.getName())) {
        return rt + pgd.getDisplayName();
    }/* w ww  .  j a  v  a2 s.  c  o  m*/
    for (IPeakAnnotationDescriptor ipad : pgd.getPeakAnnotationDescriptors()) {
        if (names.containsKey(ipad.getName())) {
            names.put(ipad.getName(), names.get(ipad.getName()) + 1);
        } else {
            names.put(ipad.getName(), 1);
        }
    }
    if (names.isEmpty()) {
        return rt + "<NA>";
    }
    if (names.size() > 1) {
        StringBuilder sb = new StringBuilder();
        for (String key : names.keySet()) {
            sb.append(key);
            sb.append(" (" + names.get(key) + ")");
            sb.append(" | ");
        }
        return rt + sb.replace(sb.length() - 1, sb.length() - 1, "").toString();
    } else {
        return rt + names.keySet().toArray(new String[0])[0];
    }
}

From source file:raptor.connector.ics.IcsUtils.java

/**
 * Filters out illegal chars, and appends a \n to the passed in message.
 * This also converts unicode chars into Maciejg format. See
 * maciejgFormatToUnicode for more info.
 *///from   w  w w.j a v a2s. co  m
public static void filterOutbound(StringBuilder message) {
    for (int i = 0; i < message.length(); i++) {
        char currentChar = message.charAt(i);
        if (LEGAL_CHARACTERS.indexOf(currentChar) == -1) {
            if (currentChar > 256) {
                int charAsInt = currentChar;
                String stringVersion = Integer.toString(charAsInt, 16);
                String replacement = "&#x" + stringVersion + ";";
                message.replace(i, i + 1, replacement);
                i += replacement.length() - 1;
            } else {
                message.deleteCharAt(i);
                i--;
            }
        }
    }
}

From source file:de.fhg.fokus.odp.portal.uploaddata.service.Worker.java

/**
 * substitutes last ',' by '}' if there's any input.
 * /* w w w. j a  va  2s .c o m*/
 * @param extrasStringBuilder
 */
private void finishParseExtras(StringBuilder extrasStringBuilder) {
    int length = extrasStringBuilder.length();
    if (length > 1) {
        extrasStringBuilder.replace(length - 1, length, "}");
    } else {
        extrasStringBuilder.append("}");
    }
}