commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gerhard Maree" <>
Subject New method in org.apache.commons.lang.StringUtils
Date Wed, 15 Aug 2007 13:59:32 GMT


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.

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
     *  for all the strings you want to replace, but much faster </p>
     * <p><code>replaceMap</code> maps search strings to replacement
     * Each key in the map will be replaced by its value in
     * </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);
        if(isBlank(key)) {
        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));
        prevIdx = idx + keyS.length();
        idx = text.indexOf(keyS, prevIdx);
        if(idx > 0) {
          indexMap.put(new Integer(idx), keyS);
      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",
      assertEquals("barbar", StringUtils.replace("foobar",
      assertEquals("bar", StringUtils.replace("bar", replaceMap2));

      assertEquals("fobar", StringUtils.replace("fobar", replaceMap1));
      assertEquals("barobar", StringUtils.replace("fooobar",
      assertEquals("barbar", StringUtils.replace("foofoo",
      assertEquals("barfar", StringUtils.replace("fooboo",
      assertEquals("barfarbarfar", StringUtils.replace("fooboofooboo",
      assertEquals("barbarfarfar", StringUtils.replace("foofoobooboo",

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message