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:com.chintans.venturebox.util.Utils.java

public static String getReadableVersion(String version) {
    try {//  w  w w .j  a v a 2s .  c  om
        String number = version.substring(version.indexOf("-") + 1, version.lastIndexOf("-"));
        String date = version.substring(version.lastIndexOf("-") + 1,
                version.endsWith(".zip") ? version.lastIndexOf(".") : version.length());

        SimpleDateFormat curFormater = new SimpleDateFormat("yyyyMMdd");
        Date dateObj = null;
        try {
            dateObj = curFormater.parse(date);
        } catch (ParseException e) {
            // ignore
        }
        SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy");

        if (dateObj == null) {
            return number;
        }
        String newDateStr = postFormater.format(dateObj);

        StringBuilder b = new StringBuilder(newDateStr);
        int i = 0;
        do {
            b.replace(i, i + 1, b.substring(i, i + 1).toUpperCase());
            i = b.indexOf(" ", i) + 1;
        } while (i > 0 && i < b.length());
        return number + " - " + b.toString();
    } catch (Exception ex) {
        // unknown version format
        return version;
    }
}

From source file:grails.plugin.searchable.internal.util.StringQueryUtils.java

/**
 * Highlights the different terms in the second query and returns a new query string.
 * This method is intended to be used with suggested queries to display the suggestion
 * to the user in highlighted format, as per Google, so the queries are expected to roughly match
 * @param first the original query/*from   w  w  w .java2s.c  o  m*/
 * @param second the second query, in which to highlight differences
 * @param highlightPattern the pattern used to highlight; should be a {@link MessageFormat} pattern where argument
 * zero is the highlighted term text
 * @return a new copy of second with term differences highlighted
 * @throws ParseException if either first or second query is invalid
 * @see #highlightTermDiffs(String, String)
 */
public static String highlightTermDiffs(String first, String second, String highlightPattern)
        throws ParseException {
    final String defaultField = "$StringQueryUtils_highlightTermDiffs$";
    Term[] firstTerms = LuceneUtils.realTermsForQueryString(defaultField, first, WhitespaceAnalyzer.class);
    Term[] secondTerms = LuceneUtils.realTermsForQueryString(defaultField, second, WhitespaceAnalyzer.class);

    if (firstTerms.length != secondTerms.length) {
        LOG.warn("Expected the same number of terms for first query [" + first + "] and second query [" + second
                + "], " + "but first query has [" + firstTerms.length + "] terms and second query has ["
                + secondTerms.length + "] terms "
                + "so unable to provide user friendly version. Returning second query as-is.");
        return second;
    }

    MessageFormat format = new MessageFormat(highlightPattern);
    StringBuilder diff = new StringBuilder(second);
    int offset = 0;
    for (int i = 0; i < secondTerms.length; i++) {
        Term firstTerm = firstTerms[i];
        Term secondTerm = secondTerms[i];
        boolean noField = defaultField.equals(secondTerm.field());
        String snippet = noField ? secondTerm.text() : secondTerm.field() + ":" + secondTerm.text();
        int pos = diff.indexOf(snippet, offset);
        if (!firstTerm.text().equals(secondTerm.text())) {
            if (!noField) {
                pos += secondTerm.field().length() + 1;
            }
            diff.replace(pos, pos + secondTerm.text().length(),
                    format.format(new Object[] { secondTerm.text() }));
        }
        offset = pos;
    }
    return diff.toString();
}

From source file:oscar.util.UtilMisc.java

public static String replace(String expression, String searchFor, String replaceWith) {
    if (expression != null) {
        StringBuilder buf = new StringBuilder(expression);
        int pos = -1;
        do {/* ww w.  ja v  a 2s.co  m*/
            pos = buf.indexOf(searchFor, pos);
            if (pos > -1) {
                buf.delete(pos, pos + searchFor.length());
                buf.insert(pos, replaceWith);
                pos += replaceWith.length();
            } else {
                return buf.toString();
            }
        } while (true);
    } else {
        return null;
    }
}

From source file:stg.utils.StringUtils.java

/**
 * A whole word is identified as a word that does not have a valid {@link Character#isJavaIdentifierPart(char)} before or after the searchStr.
 * //from  w  ww .j a  va 2 s  .  com
 * @param text in which the search and replace is needed. 
 * @param word to be searched for
 * @param newWord to be replaced with
 * @return replaced text.
 */
public static String replaceAllWholeWord(String text, String word, String newWord) {
    StringBuilder sb = new StringBuilder(text);
    int index = 0;
    while (sb.indexOf(word, index) > -1) {
        index = sb.indexOf(word, index);
        boolean notAWholeWord = false;
        if (index > 0) {
            char c = sb.charAt(index - 1);
            if (Character.isJavaIdentifierPart(c)) {
                notAWholeWord = true;
            }
            if (index + word.length() < sb.length()) {
                c = sb.charAt(index + word.length());
                if (Character.isJavaIdentifierPart(c)) {
                    notAWholeWord = true;
                }
            }
            if (!notAWholeWord) {
                sb.replace(index, index + word.length(), newWord);
            }
        } else if (index == 0) {
            if (index + word.length() < sb.length()) {
                char c = sb.charAt(index + word.length());
                if (Character.isJavaIdentifierPart(c)) {
                    notAWholeWord = true;
                }
            }
            if (!notAWholeWord) {
                sb.replace(0, word.length(), newWord);
            }
        }
        index++;
    }
    return sb.toString();
}

From source file:stg.utils.StringUtils.java

/**
 * A whole word is identified as a word that does not have a valid {@link Character#isJavaIdentifierPart(char)} before or after the searchStr.
 * //from ww  w  .  j  ava 2s .  c  om
 * @param text in which the search and replace is needed. 
 * @param word to be searched for
 * @return total count of whole words found within the text.
 */
public static int countAllWholeWord(String text, String word) {
    StringBuilder sb = new StringBuilder(text);
    int count = 0;
    int index = 0;
    while (sb.indexOf(word, index) > -1) {
        index = sb.indexOf(word, index);
        boolean notAWholeWord = false;
        if (index > 0) {
            char c = sb.charAt(index - 1);
            if (Character.isJavaIdentifierPart(c)) {
                notAWholeWord = true;
            }
            if (index + word.length() < sb.length()) {
                c = sb.charAt(index + word.length());
                if (Character.isJavaIdentifierPart(c)) {
                    notAWholeWord = true;
                }
            }
            if (!notAWholeWord) {
                count++;
            }
        } else if (index == 0) {
            if (index + word.length() < sb.length()) {
                char c = sb.charAt(index + word.length());
                if (Character.isJavaIdentifierPart(c)) {
                    notAWholeWord = true;
                }
            }
            if (!notAWholeWord) {
                count++;
            }
        }
        index++;
    }
    return count;
}

From source file:de.tudarmstadt.ukp.wikipedia.parser.html.HtmlWriter.java

private static String convertTags(String s) {
    if (s == null) {
        return null;
    }/*from  w  ww .java2  s .c  o m*/

    StringBuilder result = new StringBuilder(s);

    int temp;

    temp = 0;
    while ((temp = result.indexOf("<", temp)) != -1) {
        result.replace(temp, temp + 1, "&lt;");
    }

    temp = 0;
    while ((temp = result.indexOf(">", temp)) != -1) {
        result.replace(temp, temp + 1, "&gt;");
    }

    return result.toString();
}

From source file:Main.java

public static StringBuilder replaceAll(StringBuilder sourceCase, StringBuilder sourceLower, int start, int end,
        StringBuilder sb, String pattern, String tagStart, String tagEnd) {
    Log.d("sourceCase, sourceLower, start, end", sourceCase.length() + ", " + sourceLower.length() + "," + start
            + "," + end + pattern + "," + tagStart + "," + tagEnd);
    int patternLength = pattern.length();
    if (patternLength > 0) {
        int i = start;
        int j;//from w  ww .  ja  v  a  2s . com
        while (((j = sourceLower.indexOf(pattern, i)) >= 0) && ((j + patternLength) <= end)) {
            sb.append(sourceCase, i, j);
            sb.append(tagStart);
            i = j + patternLength;
            sb.append(sourceCase, j, i);
            sb.append(tagEnd);
        }
        Log.d("sourceCase, i, end", sourceCase.length() + ", " + i + ", " + end);
        sb.append(sourceCase, i, end);
        return sb;
    } else {
        return sb.append(sourceCase);
    }
}

From source file:com.jroossien.boxx.util.Str.java

/**
 * Wrap the specified string to multiple lines by adding a newline symbol '\n'
 * <p/>/*from w w  w.  j a  v  a2 s . c  o  m*/
 * <p>This does not break up words.
 * Which means, if there is a word that is longer than the wrap limit it will exceed the limit.
 *
 * @param string The string that needs to be wrapped.
 * @param length The maximum length for each line.
 * @return String with linebreaks.
 */
public static String wrapString(String string, int length) {
    StringBuilder sb = new StringBuilder(string);
    int i = 0;
    while ((i = sb.indexOf(" ", i + length)) != -1) {
        sb.replace(i, i + 1, "\n");
    }
    return sb.toString();
}

From source file:fr.landel.utils.io.FileUtils.java

/**
 * Convert all newline characters into Unix newlines.
 * //from www .  j  av  a 2s. c om
 * @param input
 *            The text to convert
 * @return The text converted
 */
public static StringBuilder convertToUnix(final StringBuilder input) {
    final StringBuilder output = new StringBuilder(input);

    int pos = 0;
    while ((pos = output.indexOf(LFCR, pos)) > -1) {
        output.replace(pos, pos + 2, L);
        pos++;
    }
    pos = 0;
    while ((pos = output.indexOf(CRLF, pos)) > -1) {
        output.replace(pos, pos + 2, L);
        pos++;
    }
    pos = 0;
    while ((pos = output.indexOf(C, pos)) > -1) {
        output.replace(pos, pos + 1, L);
        pos++;
    }

    return output;
}

From source file:fr.landel.utils.io.FileUtils.java

/**
 * Convert all newline characters into Windows newlines.
 * /*ww  w .ja v  a  2s.c o  m*/
 * @param input
 *            The text to convert
 * @return The text converted
 */
public static StringBuilder convertToWindows(final StringBuilder input) {
    final StringBuilder output = new StringBuilder(input);

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

    return output;
}