View Javadoc

1   package net.sf.statsvn.util;
2   
3   public class StringUtils {
4       /**
5        * This method is a 1.4 replacement of the String.replace(CharSequence, CharSequence) found in 1.5.
6        * @param originalPattern
7        * @param newPattern
8        * @param originalString
9        * @return
10       */
11      public static String replace(final String originalPattern, final String newPattern, final String originalString) {
12          if ((originalPattern == null) || (originalPattern.length() == 0) || (originalString == null)) {
13              return originalString;
14          }
15  
16          final StringBuffer newString = new StringBuffer(originalString.length());
17          int index = 0;
18          final int originalLength = originalPattern.length();
19          int previousIndex = 0;
20  
21          while ((index = originalString.indexOf(originalPattern, index)) != -1) {
22              newString.append(originalString.substring(previousIndex, index)).append(newPattern);
23              index += originalLength;
24              previousIndex = index;
25          }
26  
27          if (previousIndex == 0) {
28              newString.append(originalString);
29          } else if (previousIndex != originalString.length()) {
30              newString.append(originalString.substring(previousIndex));
31          }
32  
33          return newString.toString();
34      }
35  }