Return-Path: Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: (qmail 88562 invoked from network); 7 Sep 2009 04:59:23 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 7 Sep 2009 04:59:23 -0000 Received: (qmail 26510 invoked by uid 500); 7 Sep 2009 04:59:22 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 26395 invoked by uid 500); 7 Sep 2009 04:59:22 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 26385 invoked by uid 99); 7 Sep 2009 04:59:22 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 07 Sep 2009 04:59:22 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.140] (HELO brutus.apache.org) (140.211.11.140) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 07 Sep 2009 04:59:19 +0000 Received: from brutus (localhost [127.0.0.1]) by brutus.apache.org (Postfix) with ESMTP id 79F6A234C044 for ; Sun, 6 Sep 2009 21:58:57 -0700 (PDT) Message-ID: <2062222054.1252299537482.JavaMail.jira@brutus> Date: Sun, 6 Sep 2009 21:58:57 -0700 (PDT) From: "Henri Yandell (JIRA)" To: issues@commons.apache.org Subject: [jira] Commented: (LANG-502) new method StringUtils.replaceIgnoreCase (with patch) In-Reply-To: <716981944.1241513790320.JavaMail.jira@brutus> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 X-Virus-Checked: Checked by ClamAV on apache.org [ https://issues.apache.org/jira/browse/LANG-502?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12752010#action_12752010 ] Henri Yandell commented on LANG-502: ------------------------------------ Given that we have a fast replace method; one question would be whether to add a private underlying method and a last argument of boolean ignoreCase. Then have it make a couple of string variable copies and if true make the copies lowercase. A very slight performance hit for the current replace (two variable declarations to existing objects and an if statement on a boolean) and not much increase in terms of maintenance. Other option would be to decide this isn't a common enough need for performance to be critical and using regex with /i is the way to go. > new method StringUtils.replaceIgnoreCase (with patch) > ----------------------------------------------------- > > Key: LANG-502 > URL: https://issues.apache.org/jira/browse/LANG-502 > Project: Commons Lang > Issue Type: Improvement > Environment: all > Reporter: Flo > Priority: Minor > Fix For: 3.0 > > > Method implementation: > /** > * Searches for all appearances of searchString (ignoring case) in text and replaces them by replacement. > * 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. - You can reply to this email to add a comment to the issue online.