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:Main.java

public static void main(String[] arg) {

    StringBuilder buffer = new StringBuilder("from java2s.com");

    System.out.println(buffer.indexOf("ava", 2));
}

From source file:Main.java

private static StringBuilder replaceString(StringBuilder text, String search, String replace) {
    int fromIndex = 0;

    int start = text.indexOf(search, fromIndex);
    if (start == -1) {
        return text;
    }/*from  w  w w.  j  a va 2  s  .c  o m*/

    if (replace.length() > 0) {
        int end = 0;

        int endAdjust = (search.length() - replace.length());
        do {
            end = (start + replace.length()) + endAdjust;
            text.replace(start, end, replace);
            fromIndex = end;
        } while ((start = text.indexOf(search, fromIndex)) != -1);
    } else {
        do {
            text.delete(start, search.length());
            fromIndex = start + replace.length();
        } while ((start = text.indexOf(search, fromIndex)) != -1);
    }

    return text;
}

From source file:Main.java

private static void insertT9Key(@NonNull StringBuilder t9KeyBuilder, @NonNull String t9Str) {
    if (t9Str.length() == 0)
        return;//w  ww .j  a  va 2 s. c  o  m

    int index = -1;
    while ((index = t9KeyBuilder.indexOf(String.valueOf(T9_KEYS_DIVIDER), index + 1)) >= 0) {
        t9KeyBuilder.insert(index, t9Str);
        index += t9Str.length();
    }
}

From source file:Main.java

public static void replaceAll(StringBuilder sb, String source, String target) {
    int index = 0;
    int endIndex = 0;
    int length = source.length();
    while (-1 != (index = sb.indexOf(source, index))) {
        endIndex = index + length;/* w w  w  .  ja va  2s  . c  o  m*/
        sb.replace(index, endIndex, target);
        index += target.length() + 5;
    }
}

From source file:Main.java

/**
 * Notice that,//  w  w w  . j a  v  a2 s . c om
 * If we split the signature string with ", ",
 * a value (like "Google, Inc") may be broke up unexpectedly.
 * So we check "=" at the same time.
 * (AppXplore v2.5.0 makes a mistake, too.)
 *
 * An example from Google Pinyin Input:
 *     CN=Unknown, OU="Google, Inc", O="Google, Inc", L=Mountain View, ST=CA, C=US
 */
public static String analyseSignature(Principal principal, String str_nl) {
    //The result of principal.toString() is like this "x, x, x";
    StringBuilder stringBuilder = new StringBuilder(principal.toString().replaceAll(", ", str_nl));

    int index1 = 0;
    int index2;
    while (index1 >= 0) {
        if ((index2 = stringBuilder.indexOf(str_nl, index1)) < 0) {
            break;
        }
        if (!stringBuilder.substring(index1, index2).contains("=")) {
            stringBuilder.replace(index1 - str_nl.length(), index1, ", ");
        }
        index1 = stringBuilder.indexOf(str_nl, index1) + str_nl.length();
    }

    return stringBuilder.toString();
}

From source file:Main.java

public static String borraEtiqueta(String tag, String contenido, String documento) {
    StringBuilder st = new StringBuilder(documento);
    int posBusquedaInicio = 0;
    int tamanioDocumento = st.length();
    while (posBusquedaInicio < tamanioDocumento) {
        int posInicio = st.indexOf("<" + tag + ">", posBusquedaInicio);
        if (posInicio != -1) {
            String t = "</" + tag + ">";
            int posFin = st.indexOf(t, posBusquedaInicio);
            int longitudTag = t.length();
            int posContenido = documento.indexOf(contenido);
            if (posInicio < posContenido && posContenido < posFin) {
                st.delete(posInicio, posFin + longitudTag);
                return st.toString();
            }/*from  www .  ja  v a2  s  .co m*/
            posBusquedaInicio = posFin + longitudTag;

        } else {
            return documento;
        }

    }
    return documento;
}

From source file:wptools.cmds.FragToHtml.java

private static void gsub(StringBuilder buf, String old, String repl) {
    int len = old.length();
    int delta = repl.length();
    int pos = 0;/*  www .j  a  v a 2s  .co  m*/
    while (true) {
        pos = buf.indexOf(old, pos);
        if (pos < 0)
            break;
        buf.replace(pos, pos + len, repl);
        pos += delta;
    }
}

From source file:com.laxser.blitz.util.PlaceHolderUtils.java

public static String resolve(String text, Invocation inv) {
    if (StringUtils.isEmpty(text)) {
        return text;
    }/* ww  w  .ja v a  2  s  . co  m*/
    int startIndex = text.indexOf(PLACEHOLDER_PREFIX);
    if (startIndex == -1) {
        return text;
    }
    StringBuilder buf = new StringBuilder(text);
    while (startIndex != -1) {
        int endIndex = buf.indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length());
        if (endIndex != -1) {
            String placeholder = null;
            String defaultValue = null;
            for (int i = startIndex + PLACEHOLDER_PREFIX.length(); i < endIndex; i++) {
                if (buf.charAt(i) == '?') {
                    placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), i);
                    defaultValue = buf.substring(i + 1, endIndex);
                    break;
                }
            }
            if (placeholder == null) {
                placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
            }
            int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length();
            try {
                int dot = placeholder.indexOf('.');
                String attributeName = dot == -1 ? placeholder : placeholder.substring(0, dot);
                String propertyPath = dot == -1 ? "" : placeholder.substring(dot + 1);
                Object propVal = inv.getModel().get(attributeName);
                if (propVal != null) {
                    if (propertyPath.length() > 0) {
                        propVal = new BeanWrapperImpl(propVal).getPropertyValue(propertyPath);
                    }
                } else {
                    if ("flash".equals(attributeName)) {
                        propVal = inv.getFlash().get(propertyPath);
                    } else {
                        propVal = inv.getParameter(placeholder);
                    }
                }
                //
                if (propVal == null) {
                    propVal = defaultValue;
                }
                if (propVal == null) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Could not resolve placeholder '" + placeholder + "' in [" + text + "].");
                    }
                } else {
                    String toString = propVal.toString();
                    buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), toString);
                    nextIndex = startIndex + toString.length();
                }
            } catch (Throwable ex) {
                logger.warn("Could not resolve placeholder '" + placeholder + "' in [" + text + "] : " + ex);
            }
            startIndex = buf.indexOf(PLACEHOLDER_PREFIX, nextIndex);
        } else {
            startIndex = -1;
        }
    }

    return buf.toString();
}

From source file:com.foglyn.fogbugz.Utils.java

static String transformNumericAndCommonEntities(String input) {
    StringBuilder sb = new StringBuilder(input);

    int start = 0;
    for (int ampIndex = sb.indexOf("&", start); ampIndex >= 0; ampIndex = sb.indexOf("&", start)) {
        int semicolon = sb.indexOf(";", ampIndex + 1);
        if (semicolon < 0) {
            break;
        }//from ww  w  .  ja va 2 s.  co m

        start = semicolon + 1;

        String entity = sb.substring(ampIndex + 1, semicolon);

        if ("amp".equals(entity)) {
            sb.replace(ampIndex, semicolon + 1, "&");
            start = ampIndex + 1;
            continue;
        }

        if (entity.startsWith("#")) {
            try {
                int v = Integer.valueOf(entity.substring(1));
                if (v >= 0 && v <= 65535) {
                    char c = (char) v;
                    sb.replace(ampIndex, semicolon + 1, "" + c);

                    start = ampIndex + 1;
                    continue;
                }
            } catch (NumberFormatException ex) {
                // ignore
            }
        }
    }

    return sb.toString();
}

From source file:ext.sns.auth.OAuth2Client.java

/**
 * ??URI?Map// w  w w  . j ava  2s  .  c  om
 * 
 * @param uri ?URI
 * @return ?Map
 */
public static Map<String, String> getAuthBackParamByURI(String uri) {
    // ??URI1????&
    int countMatches = StringUtils.countMatches(uri, "?");
    if (countMatches > 1) {
        int firstIndex = uri.indexOf("?");

        StringBuilder uriBuilder = new StringBuilder(uri);
        for (int i = 0; i < countMatches - 1; ++i) {
            int start = uriBuilder.indexOf("?", firstIndex + 1);
            uriBuilder.replace(start, start + 1, "&");
        }
        uri = uriBuilder.toString();
    }

    String paramStr = uri.substring(uri.indexOf("?") + 1);

    Map<String, String> paramMap = new HashMap<String, String>();

    String[] pairArray = paramStr.split("&");
    for (String pair : pairArray) {
        String[] paramArray = pair.split("=");

        String val = paramArray.length == 2 ? paramArray[1] : "";
        paramMap.put(paramArray[0], val);
    }

    return paramMap;
}