Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Internationalization Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

8.3 Locale-Sensitive String Searching

RWUStringSearch allows for flexible, collator-based string searches, unlike the string searches performed by RWUString (Section 8.2). RWUString uses simple lexical comparisons of the code units in the strings, but RWUStringSearch employs the rules encapsulated by an RWUCollator and an optional RWUBreakSearch to determine if and where a match occurs.

RWUStringSearch provides a number of options to search for occurrences of the pattern string in a text string:

8.3.1 Creating an RWUStringSearch

RWUStringSearch objects are created given:

If an RWUBreakSearch is used, a substring is considered a match only if it falls on boundaries returned by the break search object. This makes it possible, for example, to search for entire words or entire sentences.

For example, this code creates an RWUStringSearch that can be used to search the RWUString text for occurrences of RWUString pattern using the string comparison rules encapsulated by RWUCollator collator:

//1

Sets the implicit conversion context to UTF-8.

//2

Creates a pattern for which to search.

//3

Creates a collator based on the default locale that ignores differences in diacritics, case, and punctuation.

8.3.2 Iteration-Style Searches

In iterator-style searches, RWUStringSearch, like RWUBreakSearch, maintains a "current" position within the source string. Immediately after construction, the current position has no meaning. A call to first() or last() sets the current position to the code unit offset just past that of the first or last match, and returns the location of the beginning of the match. The next() method advances the current position to the code unit offset immediately following that of the next match, and returns the location of the beginning of the match. The previous() method moves the current position to the beginning of the previous match, and returns the same location.

For example, this code counts the number of occurrences of pattern in text:

8.3.3 Direct Queries

RWUStringSearch supports direct match queries using the isMatch() method. This method returns true if a specified iterator offset in the search string begins a match for the pattern string.

For example, this code tests if there is a match starting at the fifth code point in a text:

8.3.4 Search and Replace

RWUStringSearch supports search and replace functionality using the replace() method. This method searches a given RWUString for matches with the pattern stored in the RWUStringSearch object. Each match is replaced with a given replacement RWUString, up to a specified number of occurrences. The default number of occurrences to replace is 1. To replace all occurrences of the pattern, specify 0 occurrences. The method returns the number of occurrences actually replaced.

For example, this code replaces all occurrences of pattern in text2 with replacement:



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.