Example usage for java.lang StringBuilder indexOf

List of usage examples for java.lang StringBuilder indexOf

Introduction

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

Prototype

@Override
    public int indexOf(String str, int fromIndex) 

Source Link

Usage

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

/**
 * Maciejg format, named after him because of his finger notes. Unicode
 * chars are represented as α β γ δ ε ζ
 * unicode equivalent \u03B1,\U03B2,.../*from   ww  w  .  j  a  v  a 2 s.  c  o m*/
 */
public static String maciejgFormatToUnicode(String inputString) {
    StringBuilder builder = new StringBuilder(inputString);
    int unicodePrefix = 0;
    while ((unicodePrefix = builder.indexOf("&#x", unicodePrefix)) != -1) {
        int endIndex = builder.indexOf(";", unicodePrefix);
        if (endIndex == -1) {
            break;
        }
        String maciejgWord = builder.substring(unicodePrefix + 3, endIndex);
        maciejgWord = StringUtils.replaceChars(maciejgWord, " \\\n", "").toUpperCase();
        if (maciejgWord.length() <= 5) {
            try {
                int intValue = Integer.parseInt(maciejgWord, 16);
                String unicode = new String(new char[] { (char) intValue });
                builder.replace(unicodePrefix, endIndex + 1, unicode);
            } catch (NumberFormatException nfe) {
                unicodePrefix = endIndex + 1;
            }
        } else {
            unicodePrefix = endIndex + 1;
        }
    }
    return builder.toString();
}

From source file:io.github.tavernaextras.biocatalogue.model.Util.java

/**
 * Makes sure that the supplied string doesn't have any lines (separated by HTML line break tag) longer
 * than specified; assumes that there are no line breaks in the source line.
 * /*from w  w w.  j a  va2 s.c  o m*/
 * @param str The string to work with.
 * @param iLineLength Desired length of each line.
 * @param bIgnoreBrokenWords True if line breaks are to be inserted exactly each <code>iLineLength</code>
 *                           symbols (which will most likely cause broken words); false to insert line breaks
 *                           at the first space after <code>iLineLength</code> symbols since last line break.
 * @return New string with inserted HTML line breaks.
 */
public static String ensureLineLengthWithinString(String str, int iLineLength, boolean bIgnoreBrokenWords) {
    StringBuilder out = new StringBuilder(str);

    // keep inserting line breaks from end of the line till the beginning until all done
    int iLineBreakPosition = 0;
    while (iLineBreakPosition >= 0 && iLineBreakPosition < out.length()) {
        // insert line break either exactly at calculated position or 
        iLineBreakPosition += iLineLength;
        iLineBreakPosition = (bIgnoreBrokenWords ? iLineBreakPosition : out.indexOf(" ", iLineBreakPosition));

        if (iLineBreakPosition > 0 && iLineBreakPosition < out.length()) {
            out.insert(iLineBreakPosition, "<br>");
            iLineBreakPosition += 4; // -- four is the length of "<br>"
        }
    }

    return (out.toString());
}

From source file:pl.nask.hsn2.normalizers.URLNormalizerUtils.java

public static String dnsToIDN(StringBuilder str, int startIndx, int endIndx) throws URLHostParseException {
    if (str.length() == 0) {
        throw new URLHostParseException("Cannot process empty string");
    }//w ww.  ja v a 2 s. c om
    if (endIndx > str.length()) {
        endIndx = str.length();
    }
    int dd = str.indexOf("..", startIndx);
    if (dd >= 0 && dd < endIndx) {
        throw new URLHostParseException(
                "Sequence '..' is forbidden in DNS: '" + str.substring(startIndx, endIndx) + "'");
    }

    String host;
    try {
        host = IDN.toASCII(str.substring(startIndx, endIndx), IDN.USE_STD3_ASCII_RULES & IDN.ALLOW_UNASSIGNED);
        if (host.startsWith(".")) {
            throw new URLHostParseException("DNS name cannot start with .");
        }
    } catch (IllegalArgumentException e) {
        throw new URLHostParseException("Error converting DNS:" + str.toString(), e);
    }

    if (host.length() == str.length() && !EncodingType.DNS_ALLOWED.allowed(host)) {
        throw new URLHostParseException("Error converting DNS: " + str.toString());
    }

    if (host.length() == 0) {
        return host;
    }
    host = host.toLowerCase();

    str.replace(startIndx, endIndx, host);
    return host;
}

From source file:org.eclipse.recommenders.tests.jdt.JavaProjectFixture.java

public Tuple<String, Set<Integer>> findMarkers(final CharSequence content) {
    final Set<Integer> markers = Sets.newTreeSet();
    int pos = 0;/*ww  w . j a v  a2s  .co m*/
    final StringBuilder sb = new StringBuilder(content);
    while ((pos = sb.indexOf(MARKER, pos)) != -1) {
        sb.deleteCharAt(pos);
        markers.add(pos);
        ensureIsTrue(pos < sb.length());
        pos--;
    }
    return newTuple(sb.toString(), markers);
}

From source file:fr.landel.utils.commons.StringUtils.java

private static void replaceBrace(final StringBuilder output, final String text, final String replacement) {
    int index = 0;
    while ((index = output.indexOf(text, index)) > -1) {
        output.replace(index, index + text.length(), replacement);
        index += replacement.length();//from   w  w  w .ja  va  2s . co m
    }
}

From source file:org.apache.sling.commons.cache.impl.AbstractCacheManagerService.java

protected InputStream processConfig(InputStream in, Map<String, Object> properties) throws IOException {
    if (in == null) {
        return null;
    }//from ww w. j  a v a2  s.c  o m
    StringBuilder config = new StringBuilder(IOUtils.toString(in, "UTF-8"));
    in.close();
    int pos = 0;
    for (;;) {
        int start = config.indexOf("${", pos);
        if (start < 0) {
            break;
        }
        int end = config.indexOf("}", start);
        if (end < 0) {
            throw new IllegalArgumentException("Config file malformed, unterminated variable "
                    + config.substring(start, Math.min(start + 10, config.length())));
        }
        String key = config.substring(start + 2, end);
        if (properties.containsKey(key)) {
            String replacement = (String) properties.get(key);
            config.replace(start, end + 1, replacement);
            pos = start + replacement.length();
        } else {
            throw new IllegalArgumentException("Missing replacement property " + key);
        }
    }
    return new ByteArrayInputStream(config.toString().getBytes("UTF-8"));

}

From source file:com.aionengine.gameserver.cache.HTMLCache.java

private void replaceAll(StringBuilder sb, String pattern, String value) {
    for (int index = 0; (index = sb.indexOf(pattern, index)) != -1;)
        sb.replace(index, index + pattern.length(), value);
}

From source file:com.aionemu.gameserver.cache.HTMLCache.java

private void replaceAll(StringBuilder sb, String pattern, String value) {
    for (int index = 0; (index = sb.indexOf(pattern, index)) != -1;) {
        sb.replace(index, index + pattern.length(), value);
    }//  w  ww . java  2  s . c  om
}

From source file:uk.co.tfd.sm.memory.ehcache.CacheManagerServiceImpl.java

private InputStream processConfig(InputStream in, Map<String, Object> properties) throws IOException {
    if (in == null) {
        return null;
    }/* www . j  av a  2s  . co  m*/
    StringBuilder config = new StringBuilder(IOUtils.toString(in, "UTF-8"));
    in.close();
    int pos = 0;
    for (;;) {
        int start = config.indexOf("${", pos);
        if (start < 0) {
            break;
        }
        int end = config.indexOf("}", start);
        if (end < 0) {
            throw new IllegalArgumentException("Config file malformed, unterminated variable "
                    + config.substring(start, Math.min(start + 10, config.length())));
        }
        String key = config.substring(start + 2, end);
        if (properties.containsKey(key)) {
            String replacement = (String) properties.get(key);
            config.replace(start, end + 1, replacement);
            pos = start + replacement.length();
        } else {
            throw new IllegalArgumentException("Missing replacement property " + key);
        }
    }
    return new ByteArrayInputStream(config.toString().getBytes("UTF-8"));

}

From source file:org.jahia.services.render.filter.AggregateFilter.java

/**
 * Aggregate the content that are inside the fragment to get a full HTML content with all sub modules embedded.
 *
 * @param content The fragment/*from  www. j  av a  2s.  c o m*/
 * @param renderContext The render context
 */
protected String aggregateContent(String content, RenderContext renderContext) throws RenderException {
    int esiTagStartIndex = content.indexOf(ESI_TAG_START);
    if (esiTagStartIndex == -1) {
        return content;
    } else {
        StringBuilder sb = new StringBuilder(content);
        while (esiTagStartIndex != -1) {
            int esiTagEndIndex = sb.indexOf(ESI_TAG_END, esiTagStartIndex);
            if (esiTagEndIndex != -1) {
                String replacement = generateContent(renderContext,
                        sb.substring(esiTagStartIndex + ESI_TAG_START.length(), esiTagEndIndex));
                if (replacement == null) {
                    replacement = "";
                }
                sb.replace(esiTagStartIndex, esiTagEndIndex + ESI_TAG_END_LENGTH, replacement);
                esiTagStartIndex = sb.indexOf(ESI_TAG_START, esiTagStartIndex + replacement.length());
            } else {
                // no closed esi end tag found
                return sb.toString();
            }
        }
        return sb.toString();
    }
}