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:fr.landel.utils.io.FileUtils.java

/**
 * Convert all newline characters into Mac OS newlines.
 * //from  ww  w  .  j a v a  2 s.  c  om
 * @param input
 *            The text to convert
 * @return The text converted
 */
public static StringBuilder convertToMacOS(final StringBuilder input) {
    final StringBuilder output = new StringBuilder(input);

    int pos = 0;
    while ((pos = output.indexOf(CRLF, pos)) > -1) {
        output.replace(pos, pos + 2, LFCR);
        pos++;
    }
    pos = 0;
    while ((pos = output.indexOf(C, pos)) > -1) {
        if (pos == 0 || output.charAt(pos - 1) != LF) {
            output.replace(pos, pos + 1, LFCR);
        }
        pos++;
    }
    pos = 0;
    final int len = output.length();
    while ((pos = output.indexOf(L, pos)) > -1) {
        if (pos == len - 1 || output.charAt(pos + 1) != CR) {
            output.replace(pos, pos + 1, LFCR);
        }
        pos++;
    }

    return output;
}

From source file:org.omegat.util.StaticUtils.java

private static void replaceGlobs(StringBuilder haystack, String needle, String replacement) {
    replacement = "\\E" + replacement + "\\Q";
    int current = 0;
    int globIndex = 0;
    while ((globIndex = haystack.indexOf(needle, current)) != -1) {
        haystack.replace(globIndex, globIndex + 1, replacement);
        current = globIndex + replacement.length();
    }/*from  w w  w  .  jav  a2 s .co m*/
}

From source file:org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.advanced.core.AgentUtilOperations.java

public static String formatMessage(String message) {
    StringBuilder formattedMsg = new StringBuilder(message);

    ArrayList<String> keyWordList = new ArrayList<String>();
    keyWordList.add("define");
    keyWordList.add("from");
    keyWordList.add("select");
    keyWordList.add("group");
    keyWordList.add("insert");
    keyWordList.add(";");

    for (String keyWord : keyWordList) {
        int startIndex = 0;

        while (true) {
            int keyWordIndex = formattedMsg.indexOf(keyWord, startIndex);

            if (keyWordIndex == -1) {
                break;
            }/*from  w  w  w .  java  2 s  .c o  m*/

            if (keyWord.equals(";")) {
                if (keyWordIndex != 0 && (keyWordIndex + 1) != formattedMsg.length()
                        && formattedMsg.charAt(keyWordIndex + 1) == ' ') {
                    formattedMsg.setCharAt((keyWordIndex + 1), '\n');
                }
            } else {
                if (keyWordIndex != 0 && formattedMsg.charAt(keyWordIndex - 1) == ' ') {
                    formattedMsg.setCharAt((keyWordIndex - 1), '\n');
                }
            }
            startIndex = keyWordIndex + 1;
        }
    }
    return formattedMsg.toString();
}

From source file:org.ambraproject.wombat.service.CommentFormatting.java

/**
 * Given a string, and the index to start looking at, find the index of the start of the scheme. Eg.
 * <pre>//w  w w  .j av a 2s .  co m
 * getSchemeIndex("notes://abc", 0) -> 0
 * getSchemeIndex("abc notes://abc", 0) -> 4
 * </pre>
 *
 * @param str        The string to search for
 * @param startIndex Where to start looking at
 * @return The location the string was found, ot -1 if the string was not found.
 */
private static int getSchemeIndex(StringBuilder str, int startIndex) {
    int schemeIndex = str.indexOf(SCHEME_URL, startIndex + 1);

    //if it was not found, or found at the start of the string, then return 'not found'
    if (schemeIndex <= 0) {
        return -1;
    }

    //walk backwards through the scheme until we find the first non valid character
    int schemeStart;

    for (schemeStart = schemeIndex - 1; schemeStart >= 0; schemeStart--) {
        char currentChar = str.charAt(schemeStart);

        if (!isValidSchemeChar(currentChar)) {
            break;
        }
    }

    //reset the scheme to the starting character
    schemeStart++;

    /*
         we don't want to do this, otherwise an invalid scheme would ruin the linking for later schemes
        if (isValidScheme(str.substring(schemeStart, schemeIndex)))
            return schemeStart;
        else
            return -1;
    */
    return schemeStart;
}

From source file:org.opendaylight.didm.tools.utils.StringUtils.java

/**
 * A simple string formatter that replaces each occurrence of
 * <code>{}</code> in the format string with the string representation
 * of each of the subsequent, optional arguments.
 * For example:/*from w ww.  j ava2  s  .c o  m*/
 * <pre>
 *     StringUtils.format("{} = {}", "Foo", 123);
 *     // returns "Foo = 123"
 * </pre>
 *
 * @param fmt the format string
 * @param o arguments to be inserted into the output string
 * @return a formatted string
 */
public static String format(String fmt, Object... o) {
    if (fmt == null)
        throw new NullPointerException("null format string");
    if (o.length == 0)
        return fmt;

    // Format the message using the format string as the seed.
    // Stop either when the list of objects is exhausted or when
    // there are no other place-holder tokens.
    final int ftlen = FORMAT_TOKEN.length();
    int i = 0;
    int p = -1;
    String rep;
    StringBuilder sb = new StringBuilder(fmt);
    while (i < o.length && (p = sb.indexOf(FORMAT_TOKEN, p + 1)) >= 0) {
        rep = o[i] == null ? NULL_REP : o[i].toString();
        sb.replace(p, p + ftlen, rep);
        i++;
    }
    return sb.toString();
}

From source file:com.ing.connector.util.WStringUtil.java

public static List<String> splitLine(String input, int splitPosition) {
    List<String> lines = new ArrayList<String>();

    StringBuilder stringBuilder = new StringBuilder(input.trim());
    boolean linesRemaining = true;
    while (linesRemaining) {
        if (stringBuilder.length() <= splitPosition || stringBuilder.lastIndexOf(" ") < splitPosition) {
            lines.add(stringBuilder.toString());
            linesRemaining = false;//from  w  w w .j  a  va2s . c  o  m
        } else {
            int indexOfSpace = stringBuilder.indexOf(" ", splitPosition);
            String nextLine = stringBuilder.substring(0, indexOfSpace + 1);
            lines.add(nextLine);
            stringBuilder.delete(0, indexOfSpace + 1);
        }
    }

    return lines;
}

From source file:org.ambraproject.util.TextUtils.java

/**
   * Given a string, and the index to start looking at, find the index of the start of the scheme. Eg.
 * <pre>/*from   w  w  w  .ja  va  2  s.co  m*/
 * getSchemeIndex("notes://abc", 0) -> 0
 * getSchemeIndex("abc notes://abc", 0) -> 4
 * </pre>
 * @param str    The string to search for
 * @param startIndex   Where to start looking at
 * @return The location the string was found, ot -1 if the string was not found.
 */
private static int getSchemeIndex(StringBuilder str, int startIndex) {
    int schemeIndex = str.indexOf(UrlUtils.SCHEME_URL, startIndex + 1);

    //if it was not found, or found at the start of the string, then return 'not found'
    if (schemeIndex <= 0) {
        return -1;
    }

    //walk backwards through the scheme until we find the first non valid character
    int schemeStart;

    for (schemeStart = schemeIndex - 1; schemeStart >= 0; schemeStart--) {
        char currentChar = str.charAt(schemeStart);

        if (!UrlUtils.isValidSchemeChar(currentChar)) {
            break;
        }
    }

    //reset the scheme to the starting character
    schemeStart++;

    /*
         we don't want to do this, otherwise an invalid scheme would ruin the linking for later schemes
        if (UrlUtils.isValidScheme(str.substring(schemeStart, schemeIndex)))
            return schemeStart;
        else
            return -1;
    */
    return schemeStart;
}

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

public static int findFirstMatch(StringBuilder sb, String[] ss, int startIndex, int endIndex) {
    int first = endIndex;
    for (int i = 0; i < ss.length; i++) {
        int tmp = sb.indexOf(ss[i], startIndex);
        if (tmp >= 0 && tmp < first && tmp + ss[i].length() < endIndex) {
            first = tmp;/*from w ww.j ava  2s .c  o  m*/
        }
    }
    if (first < endIndex) {
        return first;
    }
    return -1;
}

From source file:com.opendoorlogistics.core.utils.strings.Strings.java

/**
 * See http://stackoverflow.com/questions/5054995/how-to-replace-case-insensitive-literal-substrings-in-java
 * /*w w  w  .ja  v  a  2  s  .c  om*/
 * @param source
 * @param target
 * @param replacement
 * @return
 */
public static String caseInsensitiveReplace(String source, String target, String replacement) {
    StringBuilder sbSource = new StringBuilder(source);
    StringBuilder sbSourceLower = new StringBuilder(source.toLowerCase());
    String searchString = target.toLowerCase();

    int idx = 0;
    while ((idx = sbSourceLower.indexOf(searchString, idx)) != -1) {
        sbSource.replace(idx, idx + searchString.length(), replacement);
        sbSourceLower.replace(idx, idx + searchString.length(), replacement);
        idx += replacement.length();
    }
    sbSourceLower.setLength(0);
    sbSourceLower.trimToSize();
    sbSourceLower = null;

    return sbSource.toString();
}

From source file:org.ambraproject.util.TextUtils.java

/**
 * Get the starting index of a URL (either 'abc://' or 'www.')
 * @param str String builder//from  w w w. ja v  a2  s.co m
 * @param startIndex index
 * @return new index
 */
private static int getStartUrl(StringBuilder str, int startIndex) {
    int schemeIndex = getSchemeIndex(str, startIndex);
    final int wwwIndex = str.indexOf("www.", startIndex + 1);

    if ((schemeIndex == -1) && (wwwIndex == -1)) {
        return -1;
    } else if (schemeIndex == -1) {
        return wwwIndex;
    } else if (wwwIndex == -1) {
        return schemeIndex;
    }

    return Math.min(schemeIndex, wwwIndex);
}