commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jara Cesnek (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (LANG-502) new method StringUtils.replaceIgnoreCase (with patch)
Date Mon, 01 Oct 2012 13:05:07 GMT

    [ https://issues.apache.org/jira/browse/LANG-502?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13466779#comment-13466779
] 

Jara Cesnek commented on LANG-502:
----------------------------------

Shame, now I'll have to write the method itself :-)
                
> new method StringUtils.replaceIgnoreCase (with patch)
> -----------------------------------------------------
>
>                 Key: LANG-502
>                 URL: https://issues.apache.org/jira/browse/LANG-502
>             Project: Commons Lang
>          Issue Type: Improvement
>          Components: lang.*
>         Environment: all
>            Reporter: Flo
>            Priority: Minor
>
> Method implementation:
> 	/**
> 	 * Searches for all appearances of <code>searchString</code> (ignoring case)
in <code>text</code> and replaces them by <code>replacement</code>.
> 	 * The difference to {@link String#replace(CharSequence, CharSequence)} and {@link StringUtils#replace(String,
String, String)} is that this implementation ignores case.
> 	 *  
> 	 * @param text The text in which to do replacements.
> 	 * @param searchString The string to remove from the text (ignoring case).
> 	 * @param replacement The string to put instead of the searchString.
> 	 * @return A new string with all searchString replaced by replacement.
> 	 * @author frickert
> 	 */
> 	public static String replaceIgnoreCase(String text, String searchString, String replacement)
> 	{
> 		String lowerCaseText = text.toLowerCase();
> 		String lowerCaseSearchString = searchString.toLowerCase();
> 		StringBuilder sb = new StringBuilder(text);
> 		int searchStart = 0;
> 		final int modifierPerReplacement = replacement.length() - searchString.length();
> 		int sbDrift = 0; // by doing replacements in sb, sb and the text drift off in length
and index in case the searchString and the replacement are of different length
> 		int finding = lowerCaseText.indexOf(lowerCaseSearchString, searchStart);
> 		while (finding >= 0)
> 		{
> 			sb.replace(finding + sbDrift, finding + sbDrift + searchString.length(), replacement);
> 			sbDrift += modifierPerReplacement;
> 			searchStart = finding + searchString.length();
> 			finding = lowerCaseText.indexOf(lowerCaseSearchString, searchStart);
> 		}
> 		return sb.toString();
> 	}
> test cases:
>     public void testReplaceIgnoreCase() throws Throwable {
>         String is;
>         is = CommonHelpers.replaceIgnoreCase("bobOBOBobOB", "Bob", "Flo");
>         assertEquals("search really ignores case", "FloOFlooFlo", is);
>         is = CommonHelpers.replaceIgnoreCase("bobOBOBobOB", "Bob", "Flo");
>         assertEquals("replacement does care about case", "FloOFlooFlo", is);
>         is = CommonHelpers.replaceIgnoreCase("bob bob bob", "Bob", "Florian");
>         assertEquals("length difference of searchString and replacement > 0", "Florian
Florian Florian", is);
>         is = CommonHelpers.replaceIgnoreCase("bob bob bob", "Bob", "Ed");
>         assertEquals("length difference of searchString and replacement < 0", "Ed
Ed Ed", is);
>         is = CommonHelpers.replaceIgnoreCase("GROSS und klein", "und", "&");
>         assertEquals("originals case is preserved in not replace chars", "GROSS &
klein", is);
>     }

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Mime
View raw message