Example usage for java.lang StringBuffer indexOf

List of usage examples for java.lang StringBuffer indexOf

Introduction

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

Prototype

@Override
public int indexOf(String str) 

Source Link

Usage

From source file:IndexOfDemo.java

public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("one two one");
    int i;//from   w w w.  j  a va 2s.c  o  m

    i = sb.indexOf("one");
    System.out.println("First index: " + i);

    i = sb.lastIndexOf("one");
    System.out.println("Last index: " + i);
}

From source file:Main.java

public static void main(String args[]) {
    StringBuffer sb = new StringBuffer();
    sb.append("java2s.com");
    int index = sb.indexOf("a");
    System.out.println(index);/*w  w  w. j a  v  a  2s . c om*/

}

From source file:org.eclim.util.StringUtils.java

/**
 * Replaces placeholders of the form ${key} in the supplied string using
 * key / value pairs from the Map provided.
 *
 * @param string the String to evaluate.
 * @param values the values to use in the string.
 * @return The evaluation result.//  ww  w.  java2 s  . co m
 */
@SuppressWarnings("rawtypes")
public static String replacePlaceholders(String string, Map values) {
    if (string == null || values == null) {
        return string;
    }

    StringBuffer buffer = new StringBuffer(string);

    int start = buffer.indexOf(PLACEHOLDER_PREFIX);
    int end = buffer.indexOf(PLACEHOLDER_SUFFIX);
    while (start != -1 && end != -1) {
        String placeholder = buffer.substring(start + PLACEHOLDER_PREFIX.length(), end);

        Object value = values.get(placeholder);
        if (value != null) {
            buffer.replace(start, end + 1, value.toString());
        }

        start = buffer.indexOf(PLACEHOLDER_PREFIX, start + 1);
        end = buffer.indexOf(PLACEHOLDER_SUFFIX, start + 1);
    }

    return buffer.toString();
}

From source file:Main.java

/**
 * XML encode a text string.//from w ww  .  j  a  v a 2 s. c om
 * 
 * @param text - a String.
 * @return an XML encoded text string.
 */
public static String xmlRemoveEscapedString(String text) {
    if (text == null)
        return text;
    else {
        StringBuffer sb = new StringBuffer(text);

        int i = sb.indexOf("
");
        while (i != -1) {
            sb.replace(i, i + 5, "\r");
            i = sb.indexOf("
");
        }

        i = sb.indexOf("<");
        while (i != -1) {
            sb.replace(i, i + 4, "<");
            i = sb.indexOf("&lt;");
        }

        i = sb.indexOf("&gt;");
        while (i != -1) {
            sb.replace(i, i + 4, ">");
            i = sb.indexOf("&gt;");
        }

        i = sb.indexOf("&quot;");
        while (i != -1) {
            sb.replace(i, i + 6, "\"");
            i = sb.indexOf("&quot;");
        }

        i = sb.indexOf("&apos;");
        while (i != -1) {
            sb.replace(i, i + 6, "\'");
            i = sb.indexOf("&apos;");
        }

        i = sb.indexOf("&amp;");
        while (i != -1) {
            sb.replace(i, i + 5, "&");
            i = sb.indexOf("&amp;");
        }
        return sb.toString();
    }
}

From source file:com.enonic.cms.business.portal.rendering.tracing.TraceMarkerHelper.java

/**
 * This method modifies the markup with extra trace code.
 *///from   ww  w  .j  av  a 2s.  com
public static String writePageMarker(RenderTraceInfo traceInfo, String markup, String outputMethod) {
    outputMethod = outputMethod.toLowerCase();
    boolean usePageMarker = outputMethod.contains("html");

    if ((traceInfo != null) && usePageMarker) {
        StringBuffer buffer = new StringBuffer(markup);
        int pos = buffer.indexOf("</head>");
        if (pos > -1) {
            String href = "__info__?type=css&key=" + traceInfo.getKey();
            href = makeXmlCompliantIfNeeded(href, outputMethod);
            buffer.insert(pos, writeCssInclude(href));
        }

        pos = buffer.indexOf("</body>");
        if (pos > -1) {
            String href = "__info__?type=javascript&key=" + traceInfo.getKey();
            href = makeXmlCompliantIfNeeded(href, outputMethod);
            buffer.insert(pos, writeJavaScriptInclude(href));
        }

        return buffer.toString();
    } else {
        return markup;
    }
}

From source file:com.enonic.cms.core.portal.rendering.tracing.TraceMarkerHelper.java

/**
 * This method modifies the markup with extra trace code.
 */// w  w w  .  j  a v a 2  s .  co  m
public static String writePageMarker(RenderTraceInfo traceInfo, String markup, String outputMethod) {
    if (outputMethod == null) {
        return markup;
    }

    outputMethod = outputMethod.toLowerCase();
    boolean usePageMarker = outputMethod.contains("html");

    if ((traceInfo != null) && usePageMarker) {
        StringBuffer buffer = new StringBuffer(markup);
        int pos = buffer.indexOf("</head>");
        if (pos > -1) {
            String href = "__info__?type=css&key=" + traceInfo.getKey();
            href = makeXmlCompliantIfNeeded(href, outputMethod);
            buffer.insert(pos, writeCssInclude(href));
        }

        pos = buffer.indexOf("</body>");
        if (pos > -1) {
            String href = "__info__?type=javascript&key=" + traceInfo.getKey();
            href = makeXmlCompliantIfNeeded(href, outputMethod);
            buffer.insert(pos, writeJavaScriptInclude(href));
        }

        return buffer.toString();
    } else {
        return markup;
    }
}

From source file:com.jnj.b2b.storefront.util.MetaSanitizerUtil.java

/**
 * Takes a string of words, removes duplicates and returns a comma separated list of keywords as a String
 * // www. j  a  v a  2s .  com
 * @param keywords
 *           Keywords to be sanitized
 * @return String of comma separated keywords
 */
public static String sanitizeKeywords(final String keywords) {
    final String clean = (StringUtils.isNotEmpty(keywords) ? Jsoup.parse(keywords).text() : ""); // Clean html
    final String[] words = StringUtils.split(clean.replace("\"", "")); // Clean quotes

    // Remove duplicates
    final StringBuffer noDupes = new StringBuffer();
    for (final String word : words) {
        if (noDupes.indexOf(word) == -1) {
            noDupes.append(word).append(',');
        }
    }
    if (noDupes.length() > 0) {
        noDupes.deleteCharAt(noDupes.length() - 1);
    }
    return noDupes.toString();
}

From source file:com.aw.core.dao.AWQueryRunner.java

public static String buildSQLLog(String sql, Object[] filterKeys) {
    StringBuffer buf = new StringBuffer();
    buf.append(sql);/*from  w ww  .ja v a2s  . c o  m*/
    int idx = 0;
    while (buf.indexOf("?") >= 0) {
        int pos = buf.indexOf("?");
        buf.deleteCharAt(pos);
        Object filterKey = filterKeys[idx];
        if (filterKey instanceof String)
            filterKey = "'" + filterKey + "'";
        buf.insert(pos, filterKey);
        idx++;
    }
    //        if (filterKeys != null) {
    //            buf.append(" params:");
    //            buf.append(Arrays.asList(filterKeys).toString());
    //        }
    return buf.toString();
}

From source file:org.ops4j.pax.runner.commons.properties.SystemPropertyUtils.java

/**
 * Resolve ${...} placeholders in the given text, replacing them with corresponding property values or system
 * property values./*from w  w w .ja  va  2 s .  c  o  m*/
 *
 * @param text       the String to resolve
 * @param properties properties to be searched beside system properties
 *
 * @return the resolved String
 *
 * @see #PLACEHOLDER_PREFIX
 * @see #PLACEHOLDER_SUFFIX
 */
public static String resolvePlaceholders(final String text, final Properties properties) {
    if (text == null) {
        return null;
    }
    StringBuffer buf = new StringBuffer(text);

    int startIndex = buf.indexOf(PLACEHOLDER_PREFIX);
    while (startIndex != -1) {
        int endIndex = buf.indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length());
        if (endIndex != -1) {
            String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
            int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length();
            try {
                String propVal = properties.getProperty(placeholder);
                if (propVal == null) {
                    propVal = System.getProperty(placeholder);
                    if (propVal == null) {
                        // Fall back to searching the system environment.
                        propVal = System.getenv(placeholder);
                    }
                }
                if (propVal != null) {
                    buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal);
                    nextIndex = startIndex + propVal.length();
                } else {
                    if (LOGGER.isWarnEnabled()) {
                        LOGGER.warn("Could not resolve placeholder '" + placeholder + "' in [" + text
                                + "] as system property: neither system property nor environment variable found");
                    }
                }
            } catch (Throwable ex) {
                if (LOGGER.isWarnEnabled()) {
                    LOGGER.warn("Could not resolve placeholder '" + placeholder + "' in [" + text
                            + "] as system property: " + ex);
                }
            }
            startIndex = buf.indexOf(PLACEHOLDER_PREFIX, nextIndex);
        } else {
            startIndex = -1;
        }
    }

    return buf.toString();
}

From source file:org.exoplatform.wiki.rendering.internal.parser.confluence.ConfluenceLinkReferenceParser.java

public static String divideOn(StringBuffer buffer, char divider) {
    if (buffer.length() == 0) {
        return null;
    }//w  ww  .  jav a  2  s. co  m
    int i = buffer.indexOf(Character.toString(divider));

    if (i < 0) {
        return null;
    }
    if (i == 0) {
        buffer.deleteCharAt(0);
        return null;
    }

    String body = buffer.substring(0, i);
    buffer.delete(0, i + 1);
    return body;
}