You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Santhosh Kumar <sa...@autodesk.com> on 2008/09/16 00:49:48 UTC

[LANG] StringUtils.extractXXXString methods

I have been thinking that extractXXXString methods would be a great addition to StringUtils class, which lets user to extract a String matching specified regular expression character class from specified index (in direct or reverse order). Other convenience methods can be built on top of this like extractInt or extractDouble, which would extract the appropriate value from the start/end/index.

Here is the javadoc for underlying bottom method. Let me know what you guys think. I have written all methods and completed the test cases for all of these methods so I can send out the patch file if everybody agrees.

Santhosh.

    /**
     * Extracts the continues sequence of characters from specified String <code>str</code>,
     * starting at specified index <code>startIndex</code>, which matches the specified regular
     * expression <code>charsRegex</code>.
     * <p>
     * <code>null</code>s and out of bounds index values are handled gracefully. Searching is ended when
     * end of string is reached or when a non-matching character is found. If search is required to be performed
     * ignoring the case, appropriate character class needs to be included in the specified regular expression.
     * <p>
     * Some examples:
     * <pre>
     * StringUtils.extractString(null, null, -1, false) = null
     * StringUtils.extractString(null, "[a-z]", 0, false) = null
     * StringUtils.extractString("SomeString", null, 0, false) = null
     * StringUtils.extractString("SomeString", null, 0, false) = null
     * StringUtils.extractString("SomeString", null, 0, false) = null
     * StringUtils.extractString("SomeString", "s", 0, false) = ""
     * StringUtils.extractString("SomeString", "S", 0, false) = "S"
     * StringUtils.extractString("SomeString", "[a-z]", 0, false) = ""
     * StringUtils.extractString("SomeString", "[a-z]", 1, false) = "ome"
     * StringUtils.extractString("SomeString", "[a-zA-Z]", 1, false) = "omeString"
     * StringUtils.extractString("SomeString", "[a-zA-Z]", 0, false) = "SomeString"
     * StringUtils.extractString("SomeString-456", "[0-9]", 0, false) = ""
     * StringUtils.extractString("SomeString-456", "[0-9]", Integer.MAX_VALUE, true) = "456"
     * </pre>
     *
     * @param str the <code>String</code> to be searched. If <code>null</code>, method returns <code>null</code>
     * @param charsRegex the regular expression to match the characters. If <code>null</code>, method returns <code>null</code>
     * @param startIndex the starting index of the search. If index is &lt; 0, initialized to 0. If index is &gt;= str.length(),
     *                          initialized to str.length() - 1
     * @param reverse if <code>true</code> characters will be searched in the reverse order start from <code>startIndex</code>
     *
     * @return the matched characters as <code>String</code>. <code>null</code> if <code>str</code> or <code>charsRegex</code> is <code>null</code>
     * and empty <code>String</code> ("") if there are no matching characters found.
     */

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org