01: /*
02: * Copyright 2007 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package edu.yale.its.tp.cas.util;
17:
18: /**
19: * <p>
20: * A class housing some utility functions related to String manipulation.
21: * </p>
22: * <p>
23: * Copyright 2000, Shawn Bayern.
24: * </p>
25: */
26: public class StringUtil {
27:
28: /**
29: * Replaces all occurrences of an old String with a new String in the given String.
30: */
31: public static String substituteAll(String s, String o, String n) {
32: if (s == null)
33: return null;
34: while (s.indexOf(o) != -1)
35: s = substituteOne(s, o, n);
36: return s;
37: }
38:
39: /**
40: * Replaces one occurrence of an old String with a new String in the given String.
41: */
42: public static String substituteOne(String s, String o, String n) {
43: if (s == null)
44: return null;
45: int begin = s.indexOf(o);
46: if (begin == -1)
47: return s;
48: int end = begin + o.length();
49: return (new StringBuffer(s)).replace(begin, end, n).toString();
50: }
51: }
|