/** * Returns the text that was matched by the most recent call to * {@link #first}, {@link #next}, {@link #previous}, or {@link #last}. * If the iterator is not pointing at a valid match (e.g. just after * construction or after {@link #DONE} has been returned, * returns an empty string. * * @return the substring in the target test of the most recent match, * or null if there is no match currently. * @see #first * @see #next * @see #previous * @see #last * @stable ICU 2.0 */ public String getMatchedText() { if (search_.matchedLength() > 0) { int limit = search_.matchedIndex_ + search_.matchedLength(); StringBuilder result = new StringBuilder(search_.matchedLength()); CharacterIterator it = search_.text(); it.setIndex(search_.matchedIndex_); while (it.getIndex() < limit) { result.append(it.current()); it.next(); } it.setIndex(search_.matchedIndex_); return result.toString(); } return null; }
/** * @internal * @deprecated This API is ICU internal only. */ @Deprecated protected void setMatchNotFound() { super.setMatchNotFound(); // SearchIterator#setMatchNotFound() does following: // search_.matchedIndex_ = DONE; // search_.setMatchedLength(0); if (search_.isForwardSearching_) { textIter_.setOffset(search_.text().getEndIndex()); } else { textIter_.setOffset(0); } }
/** * Sets the {@link RuleBasedCollator} to be used for language-specific searching. * <p> * The iterator's position will not be changed by this method. * @param collator to use for this <tt>StringSearch</tt> * @throws IllegalArgumentException thrown when collator is null * @see #getCollator * @stable ICU 2.0 */ public void setCollator(RuleBasedCollator collator) { if (collator == null) { throw new IllegalArgumentException("Collator can not be null"); } collator_ = collator; ceMask_ = getMask(collator_.getStrength()); ULocale collLocale = collator.getLocale(ULocale.VALID_LOCALE); search_.internalBreakIter_ = BreakIterator.getCharacterInstance(collLocale == null ? ULocale.ROOT : collLocale); search_.internalBreakIter_.setText((CharacterIterator)search_.text().clone()); // We need to create a clone toShift_ = collator.isAlternateHandlingShifted(); variableTop_ = collator.getVariableTop(); textIter_ = new CollationElementIterator(pattern_.text_, collator); utilIter_ = new CollationElementIterator(pattern_.text_, collator); // initialize() _after_ setting the iterators for the new collator. initialize(); }
textIter_.setText(search_.text());
/** * Set the BreakIterator that will be used to restrict the points * at which matches are detected. * * @param breakiter A BreakIterator that will be used to restrict the * points at which matches are detected. If a match is * found, but the match's start or end index is not a * boundary as determined by the {@link BreakIterator}, * the match will be rejected and another will be searched * for. If this parameter is <tt>null</tt>, no break * detection is attempted. * @see BreakIterator * @stable ICU 2.0 */ public void setBreakIterator(BreakIterator breakiter) { search_.setBreakIter(breakiter); if (search_.breakIter() != null) { // Create a clone of CharacterItearator, so it won't // affect the position currently held by search_.text() if (search_.text() != null) { search_.breakIter().setText((CharacterIterator)search_.text().clone()); } } }
/** * Return the string text to be searched. * @return text string to be searched. * @stable ICU 2.0 */ public CharacterIterator getTarget() { return search_.text(); }