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) 

Source Link

Usage

From source file:com.counter.counter.api.MainController.java

private List<Word> findTopWords() {
    Map<String, Word> countMap = new HashMap<String, Word>();
    StringBuilder tempSource = new StringBuilder(sourceText.toString());
    StringBuilder tempSource1 = new StringBuilder();
    StringBuilder tempSource2 = new StringBuilder();
    for (int i = tempSource.indexOf("."); i >= 0; i = tempSource.indexOf(".", i + 1))
        tempSource1 = tempSource.deleteCharAt(i);
    for (int i = tempSource1.indexOf(","); i >= 0; i = tempSource1.indexOf(",", i + 1))
        tempSource2 = tempSource1.deleteCharAt(i);
    String line = tempSource2.toString();
    //while ((line = reader.readLine()) != null) {
    String[] words = line.split(" ");
    for (String word : words) {

        word = word.toLowerCase();//from w  w  w . j ava2  s. c om
        if ("".equals(word)) {
            continue;
        }

        Word wordObj = countMap.get(word);
        if (wordObj == null) {
            wordObj = new Word();
            wordObj.word = word;
            wordObj.count = 0;
            countMap.put(word, wordObj);
        }

        wordObj.count++;
    }
    //SortedSet<Word> sortedWords = new TreeSet<Word>(countMap.values());
    List<Word> l = new ArrayList<>(countMap.values());
    Collections.sort(l);
    return l;
}

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

/**
 * Injects all arguments in the specified char sequence. The arguments are
 * injected by replacement of keys between braces. To exclude keys, just
 * double braces (like {{key}} will return {key}). If some keys aren't
 * found, they are ignored./*from w ww  . ja  va  2s  . c o  m*/
 * 
 * <p>
 * precondition: {@code charSequence} cannot be {@code null}
 * </p>
 * 
 * <pre>
 * StringUtils.injectKeys(Pair.of("${", "}"), Pair.of("${{", "}}"), "", Pair.of("key", "test"));
 * // =&gt; ""
 * 
 * StringUtils.injectKeys(Pair.of("${", "}"), Pair.of("${{", "}}"), "I'll go to the {where} this {when}", Pair.of("where", "beach"),
 *         Pair.of("when", "afternoon"));
 * // =&gt; "I'll go to the beach this afternoon"
 * 
 * StringUtils.injectKeys(Pair.of("${", "}"), Pair.of("${{", "}}"), "I'll go to {key}{{key}}{key}", Pair.of("key", "beach"));
 * // =&gt; "I'll go to beach{key}beach"
 * </pre>
 * 
 * @param include
 *            the characters that surround the property key to replace
 * @param exclude
 *            the characters that surround the property key to exclude of
 *            replacement
 * @param charSequence
 *            the input char sequence
 * @param arguments
 *            the pairs to inject
 * @param <T>
 *            the type arguments
 * @return the result with replacements
 */
@SafeVarargs
public static <T extends Map.Entry<String, Object>> String injectKeys(final Pair<String, String> include,
        final Pair<String, String> exclude, final CharSequence charSequence, final T... arguments) {

    checkParamsInjectKeys(include, exclude, charSequence);

    if (isEmpty(charSequence) || arguments == null || arguments.length == 0) {
        return charSequence.toString();
    }

    final StringBuilder output = new StringBuilder(charSequence);

    // if no brace, just returns the string
    if (output.indexOf(include.getLeft()) < 0) {
        return output.toString();
    }

    for (T argument : arguments) {
        if (argument != null) {
            int index = 0;

            final String key = new StringBuilder(include.getLeft()).append(argument.getKey())
                    .append(include.getRight()).toString();
            final String keyExclude = new StringBuilder(exclude.getLeft()).append(argument.getKey())
                    .append(exclude.getRight()).toString();

            // replace the excluded braces by a temporary string
            while ((index = output.indexOf(keyExclude, index)) > -1) {
                output.replace(index, index + keyExclude.length(), TEMP_REPLACEMENT);
            }

            // replace the key by the argument
            index = 0;
            while ((index = output.indexOf(key, index)) > -1) {
                output.replace(index, index + key.length(), String.valueOf(argument.getValue()));
            }

            // replace the temporary string by the excluded braces
            index = 0;
            while ((index = output.indexOf(TEMP_REPLACEMENT, index)) > -1) {
                output.replace(index, index + TEMP_REPLACEMENT.length(), key);
            }
        }
    }

    return output.toString();
}

From source file:net.jforum.formatters.BBConfigFormatter.java

/**
 * Formats only the [code] tag/*from ww w. ja v  a 2  s  .  c om*/
 *
 * @param text the text to format
 * @return the formatted text
 */
private String processCodeTag(String text) {
    for (BBCode bb : this.bbTags.values()) {
        // There is "code" and "code-highlight"
        if (bb.getTagName().startsWith("code")) {
            Matcher matcher = Pattern.compile(bb.getRegex()).matcher(text);
            StringBuilder sb = new StringBuilder(text);

            while (matcher.find()) {
                String lang = null;
                String contents = null;

                if ("code".equals(bb.getTagName())) {
                    contents = matcher.group(1);
                } else {
                    lang = matcher.group(1);
                    contents = matcher.group(2);
                }

                contents = StringUtils.replace(contents, "<br/> ", "\n");

                // XML-like tags
                contents = StringUtils.replace(contents, "<", "&lt;");
                contents = StringUtils.replace(contents, ">", "&gt;");

                // Note: there is no replacing for spaces and tabs as
                // we are relying on the Javascript SyntaxHighlighter library
                // to do it for us

                StringBuilder replace = new StringBuilder(bb.getReplace());
                int index = replace.indexOf("$1");

                if ("code".equals(bb.getTagName())) {
                    if (index > -1) {
                        replace.replace(index, index + 2, contents.toString());
                    }

                    index = sb.indexOf("[code]");
                } else {
                    if (index > -1) {
                        replace.replace(index, index + 2, lang.toString());
                    }

                    index = replace.indexOf("$2");

                    if (index > -1) {
                        replace.replace(index, index + 2, contents.toString());
                    }

                    index = sb.indexOf("[code=");
                }

                int lastIndex = sb.indexOf("[/code]", index) + "[/code]".length();

                if (lastIndex > index) {
                    sb.replace(index, lastIndex, replace.toString());
                }
            }

            text = sb.toString();
        }
    }

    return text;
}

From source file:fr.free.pierre.reliquet.trackit.utils.InformationFinder.java

private String internalFindTitleFromBarcode(String barcode, String extension) throws Exception {
    URL url = new URL(SEARCH_LINK + extension + SEARCH_LINK_END + barcode);
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    StringBuilder result = new StringBuilder();
    String parsing;/*from www . j  a  v a 2  s . c  om*/
    String line = "";
    try {
        while ((line = in.readLine()) != null) {
            result.append(line);
        }
        parsing = result.substring(result.indexOf(START_PATTERN) + START_PATTERN.length());
        parsing = parsing.substring(0, parsing.indexOf(END_PATTERN));
        parsing = parsing.substring(parsing.indexOf(START_SUB_PATTERN) + START_SUB_PATTERN.length());
        parsing = parsing.substring(parsing.indexOf(SUB_START_SUB_PATTERN) + SUB_START_SUB_PATTERN.length());
        parsing = parsing.substring(0, parsing.indexOf(END_SUB_PATTERN));
    } catch (Exception e) {
        if (BuildConfig.DEBUG) {
            Log.e("PARSING", "An error occured while searching the information over the internet");
            e.printStackTrace();
        }
        parsing = "";
    } finally {
        in.close();
    }
    return StringEscapeUtils.unescapeHtml(parsing);
}

From source file:com.github.ibm.domino.client.BaseClient.java

protected String getDateParameter(ZonedDateTime value) {
    StringBuilder sValue = new StringBuilder(value.format(DateTimeFormatter.ISO_DATE_TIME));
    int i = sValue.indexOf(GMT_STRING);
    if (i >= 0) {
        sValue.delete(i, i + GMT_STRING.length());
    }/*from w  w  w  . j  a va  2 s  .c om*/
    i = sValue.indexOf(PERIOD);
    if (i >= 0) {
        sValue.delete(i, i + 4);
    }
    return sValue.toString();
}

From source file:ca.uhn.fhir.rest.client.apache.ApacheHttpClient.java

private void addHeaderIfNoneExist(IHttpRequest result) {
    if (myIfNoneExistParams != null) {
        StringBuilder b = newHeaderBuilder(myUrl);
        BaseHttpClientInvocation.appendExtraParamsWithQuestionMark(myIfNoneExistParams, b,
                b.indexOf("?") == -1);
        result.addHeader(Constants.HEADER_IF_NONE_EXIST, b.toString());
    }//from w w  w. j  a va  2s.  co  m

    if (myIfNoneExistString != null) {
        StringBuilder b = newHeaderBuilder(myUrl);
        b.append(b.indexOf("?") == -1 ? '?' : '&');
        b.append(myIfNoneExistString.substring(myIfNoneExistString.indexOf('?') + 1));
        result.addHeader(Constants.HEADER_IF_NONE_EXIST, b.toString());
    }
}

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

public String execute(String previousOut, RenderContext renderContext, Resource resource, RenderChain chain)
        throws Exception {
    if (handlers != null && handlers.length > 0) {
        final String thisuuid = StringUtils.leftPad(Integer.toHexString(resource.hashCode()), 8, "0");
        Map<String, String> alreadyParsedFragmentsMap = new HashMap<>();
        StringBuilder sb = null;
        int i;/*from ww w  .  ja  va  2  s  . com*/
        if (previousOut.indexOf(TEMP_START_TAG) > -1) {
            sb = new StringBuilder(previousOut);

            // store all the already processed url traverse
            while ((i = sb.indexOf(TEMP_START_TAG)) > -1) {
                String uuid = sb.substring(i + TEMP_START_TAG.length(), i + TEMP_START_TAG.length() + 8);
                final String endTag = TEMP_END_TAG + uuid + TEMP_CLOSING;
                int j = sb.indexOf(endTag);
                alreadyParsedFragmentsMap.put(uuid, sb.substring(i + TEMP_FULL_START_TAG.length(), j));
                sb.delete(i, j + endTag.length());
                sb.insert(i, "\"/>").insert(i, uuid).insert(i, REPLACED_START_TAG);
            }
        }

        // traverse the fragment and wrap it with a temp tag
        sb = new StringBuilder(urlTraverser.traverse(sb == null ? previousOut : sb.toString(), renderContext,
                resource, handlers));
        sb.insert(0, TEMP_CLOSING).insert(0, thisuuid).insert(0, TEMP_START_TAG);
        sb.append(TEMP_END_TAG).append(thisuuid).append(TEMP_CLOSING);

        // replace all the previous stored fragments
        while ((i = sb.indexOf(REPLACED_START_TAG)) > -1) {
            String uuid = sb.substring(i + REPLACED_START_TAG.length(), i + REPLACED_START_TAG.length() + 8);
            sb.replace(i, i + REPLACED_START_TAG.length() + 8 + 3, alreadyParsedFragmentsMap.get(uuid));
        }
        return sb.toString();
    }

    return previousOut;
}

From source file:com.johncroth.histo.logging.LogHistogramWriterParserTest.java

private String makeFile(boolean includeErrors) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    pw.println("Preamble");
    if (includeErrors) { // duplicate start or missing json line
        pw.println(LogHistogramWriter.MESSAGE_START_LINE);
    }/*from w w w .  j  av a2s  . com*/
    pw.println(writer.formatMessage(new RecordedInterval<LogHistogramRecorder>(makeRecorder(), 100, 200)));
    pw.println(writer
            .formatMessage(new RecordedInterval<LogHistogramRecorder>(new LogHistogramRecorder(), 200, 300)));
    pw.println("Whatever.");
    if (includeErrors) {
        StringBuilder sb = new StringBuilder(
                writer.formatMessage(new RecordedInterval<LogHistogramRecorder>(makeRecorder(), 1000, 2000)));
        sb.insert(sb.indexOf("\n") + 5, "\n");
        pw.println(sb);
        sb = new StringBuilder(
                writer.formatMessage(new RecordedInterval<LogHistogramRecorder>(makeRecorder(), 1000, 2000)));
        sb.delete(sb.indexOf("\n") + 2, sb.indexOf("\n") + 3);
        pw.println(sb);

    }
    pw.println(writer.formatMessage(new RecordedInterval<LogHistogramRecorder>(makeRecorder(), 300, 400)));
    String x = sw.toString();
    return x;
}

From source file:simplealbum.mvc.autocomplete.JTextPaneX.java

protected void colorStyledDocument(final DefaultStyledDocument document) {
    EventQueue.invokeLater(new Runnable() {

        @Override//from   ww w .  j  av a  2s.  co  m
        public void run() {
            String input = "";
            try {
                input = document.getText(0, document.getLength());
            } catch (BadLocationException ex) {
                Logger.getLogger(JTextPaneX.class.getName()).log(Level.SEVERE, null, ex);
            }

            StringBuilder inputMut = new StringBuilder(input);
            String[] split = StringUtils.split(inputMut.toString());
            int i = 0;
            for (String string : split) {
                int start = inputMut.indexOf(string);
                int end = start + string.length();
                inputMut.replace(start, end, StringUtils.repeat(" ", string.length()));
                document.setCharacterAttributes(start, string.length(), styles[i++ % styles.length], true);
            }
        }
    });
}

From source file:ru.runa.wfe.presentation.hibernate.HibernateCompilerInheritanceFiltersBuilder.java

/**
 * Injects SQL statements to filter by fields with inheritance into SQL query.
 * /*ww  w.  ja v a 2 s . c om*/
 * @param sqlQuery
 *            SQL query to inject filter statements.
 */
public void injectFiltersStatements(StringBuilder sqlQuery) {
    if (!hqlBuilder.isFilterByInheritance()) {
        return;
    }
    List<String> filters = buildFiltersStatements();
    int insertIdx = sqlQuery.indexOf(" where ");
    if (insertIdx > 0) {
        insertIdx = sqlQuery.indexOf(" order ");
        if (insertIdx == -1) {
            insertIdx = sqlQuery.length();
        }
    } else {
        insertIdx = sqlQuery.indexOf(" order by ");
        if (insertIdx == -1) {
            insertIdx = sqlQuery.length();
        }
        sqlQuery.insert(insertIdx, " where (1=1) ");
        insertIdx += 13;
    }
    for (String filter : filters) {
        sqlQuery.insert(insertIdx, ")").insert(insertIdx, filter).insert(insertIdx, " and (");
    }
}