You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Henri Yandell (JIRA)" <ji...@apache.org> on 2007/10/25 08:27:50 UTC

[jira] Closed: (LANG-352) New method replace(String text, Map replaceMap) in StringUtils

     [ https://issues.apache.org/jira/browse/LANG-352?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell closed LANG-352.
------------------------------

    Resolution: Won't Fix

Resolving as Won't Fix because it duplicates existing behaviour - please reopen if you find time to look into StrSubstitutor and have a reason for the new code to go in.

> New method replace(String text, Map replaceMap) in StringUtils
> --------------------------------------------------------------
>
>                 Key: LANG-352
>                 URL: https://issues.apache.org/jira/browse/LANG-352
>             Project: Commons Lang
>          Issue Type: New Feature
>    Affects Versions: 2.3, 3.0
>            Reporter: Gerhard Maree
>            Priority: Minor
>
> Hi
> I have a method that I would like to contribute to the StringUtils class. I unfortunately can't access a CVS server on the internet, so I included the method and its test in this mail. I'd appreciate it if someone can have a look and add the source if it is acceptable. 
> The method basically replaces multiple strings in a String, but is very efficient. The description of the method is in the Javadoc.
> Regards,
> Gerhard
>  
> Here is method to add to org.apache.commons.lang.StringUtils
>     /**
>      * <p>Replace all occurrences of multiple strings in a string.</p>
>      * 
>      * <p>It is functionally equivalent to calling 
>      *  replaceAll(searchString, replaceString) repeatedly on <code>text</code> 
>      *  for all the strings you want to replace, but much faster </p>
>      *  
>      * <p><code>replaceMap</code> maps search strings to replacement strings.
>      * Each key in the map will be replaced by its value in <code>text</code>.
>      * </p>
>      * 
>      * @param text String to replace strings in. May be null
>      * @param replaceMap Maps search strings to replacement strings.  
>      * @return string with all values replaced. <code>null</code> if 
>      *          text was null
>      */
>     public static String replace(String text, Map replaceMap) {
>       if(isEmpty(text)) {
>         return text;
>       }
>       StringBuffer buff = new StringBuffer(text.length());
>       //map each replace string and it's next position in text
>       TreeMap indexMap = new TreeMap();
>       //populate indexMap with it's initial values
>       for (Iterator iter = replaceMap.keySet().iterator(); iter.hasNext();) {
>         String key = (String) iter.next();
>         if(isBlank(key)) {
>           continue;
>         }
>         int idx = text.indexOf(key);
>         if(idx >= 0) {
>           indexMap.put(new Integer(idx), key);
>         }
>       }
>       
>       //if there is nothing to replace
>       if(indexMap.isEmpty()) return text;
>       
>       int prevIdx = 0;
>       while(indexMap.size() > 0) {
>         Integer idxI = (Integer)indexMap.firstKey();
>         int idx = idxI.intValue();
>         String keyS = (String)indexMap.remove(idxI);
>         buff.append(text.substring(prevIdx, idx));
>         buff.append((String)replaceMap.get(keyS));
>         prevIdx = idx + keyS.length();
>         idx = text.indexOf(keyS, prevIdx);
>         if(idx > 0) {
>           indexMap.put(new Integer(idx), keyS);
>         }
>       }
>       buff.append(text.substring(prevIdx));
>       return buff.toString();
>     }
> Here is the test method to add to StringUtilsTest.
>     public void testReplace_StringMap() {
>       Map replaceMapEmpty = new HashMap();
>       Map replaceMap1 = new HashMap();
>       replaceMap1.put("foo", "bar");
>       replaceMap1.put("boo", "far");
>       Map replaceMap2 = new HashMap();
>       replaceMap2.put("foo", "");
>       Map replaceMap3 = new HashMap();
>       replaceMap3.put("", "foo");
>       
>       assertEquals(null, StringUtils.replace(null,  replaceMapEmpty));
>       assertEquals(null, StringUtils.replace(null, replaceMap1));
>       assertEquals("", StringUtils.replace("", replaceMapEmpty));
>       assertEquals("", StringUtils.replace("", replaceMap1));
>       assertEquals("foo", StringUtils.replace("foo", replaceMapEmpty));
>       assertEquals("bar", StringUtils.replace("foo", replaceMap1));
>       assertEquals("", StringUtils.replace("foo", replaceMap2));
>       assertEquals("foo", StringUtils.replace("foo", replaceMap3));
>       
>       assertEquals("foobar", StringUtils.replace("foobar", replaceMapEmpty));
>       assertEquals("barbar", StringUtils.replace("foobar", replaceMap1));
>       assertEquals("bar", StringUtils.replace("bar", replaceMap2));
>       assertEquals("fobar", StringUtils.replace("fobar", replaceMap1));
>       assertEquals("barobar", StringUtils.replace("fooobar", replaceMap1));
>       assertEquals("barbar", StringUtils.replace("foofoo", replaceMap1));
>       assertEquals("barfar", StringUtils.replace("fooboo", replaceMap1));
>       assertEquals("barfarbarfar", StringUtils.replace("fooboofooboo", replaceMap1));
>       assertEquals("barbarfarfar", StringUtils.replace("foofoobooboo", replaceMap1));
>      }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.