001: package com.sun.portal.proxylet.client.common;
002:
003: public class StringUtil {
004:
005: public static String replaceAll(final String aSource,
006: final String aFind, String aReplace) {
007: int lFindLength;
008: // the next statement has the side effect of throwing a null pointer
009: // exception if s is null.
010: if (aSource == null || aFind == null
011: || (lFindLength = aFind.length()) == 0) {
012: // If there is nothing to find, we won't try and find it.
013: return aSource;
014: }
015:
016: if (aReplace == null) {
017: // a null string and an empty string are the same
018: // for replacement purposes.
019: aReplace = "";
020: }
021:
022: int lSourceLength = aSource.length();
023: int lReplaceLength = aReplace.length();
024:
025: // We need to figure out how long our resulting string will be.
026: // This is required because without it, the possible resizing
027: // and copying of memory structures could lead to an unacceptable runtime.
028: // In the worst case it would have to be resized n times with each
029: // resize having a O(n) copy leading to an O(n^2) algorithm.
030: int length;
031: if (lFindLength == lReplaceLength) {
032: // special case in which we don't need to count the replacements
033: // because the count falls out of the length formula.
034: length = lSourceLength;
035: } else {
036: int count;
037: int start;
038: int end;
039:
040: // Scan s and count the number of times we find our target.
041: count = 0;
042: start = 0;
043: while ((end = aSource.indexOf(aFind, start)) != -1) {
044: count++;
045: start = end + lFindLength;
046: }
047:
048: if (count == 0) {
049: // special case in which on first pass, we find there is nothing
050: // to be replaced. No need to do a second pass or create a string buffer.
051: return aSource;
052: }
053:
054: length = lSourceLength
055: - (count * (lFindLength - lReplaceLength));
056: }
057:
058: int start = 0;
059: int end = aSource.indexOf(aFind, start);
060: if (end == -1) {
061: // nothing was found in the string to replace.
062: // we can get this if the find and replace strings
063: // are the same length because we didn't check before.
064: // in this case, we will return the original string
065: return aSource;
066: }
067: // it looks like we actually have something to replace
068: // *sigh* allocate memory for it.
069: StringBuffer sb = new StringBuffer(length);
070:
071: // Scan s and do the replacements
072: while (end != -1) {
073: sb.append(aSource.substring(start, end));
074: sb.append(aReplace);
075: start = end + lFindLength;
076: end = aSource.indexOf(aFind, start);
077: }
078: end = lSourceLength;
079: sb.append(aSource.substring(start, end));
080:
081: return (sb.toString());
082: }
083:
084: public static String replaceFirst(final String aSource,
085: final String aFind, String aReplace) {
086: int lFindLength;
087: // the next statement has the side effect of throwing a null pointer
088: // exception if s is null.
089: if (aSource == null || aFind == null
090: || (lFindLength = aFind.length()) == 0) {
091: // If there is nothing to find, we won't try and find it.
092: return aSource;
093: }
094:
095: if (aReplace == null) {
096: // a null string and an empty string are the same
097: // for replacement purposes.
098: aReplace = "";
099: }
100:
101: int lSourceLength = aSource.length();
102: int lReplaceLength = aReplace.length();
103:
104: // We need to figure out how long our resulting string will be.
105: // This is required because without it, the possible resizing
106: // and copying of memory structures could lead to an unacceptable runtime.
107: // In the worst case it would have to be resized n times with each
108: // resize having a O(n) copy leading to an O(n^2) algorithm.
109: int length;
110: if (lFindLength == lReplaceLength) {
111: // special case in which we don't need to count the replacements
112: // because the count falls out of the length formula.
113: length = lSourceLength;
114: } else {
115: int count;
116: int start;
117: int end;
118:
119: // Scan s and count the number of times we find our target.
120: count = 0;
121: start = 0;
122: while ((end = aSource.indexOf(aFind, start)) != -1) {
123: count++;
124: start = end + lFindLength;
125: }
126:
127: if (count == 0) {
128: // special case in which on first pass, we find there is nothing
129: // to be replaced. No need to do a second pass or create a string buffer.
130: return aSource;
131: }
132:
133: length = lSourceLength
134: - (count * (lFindLength - lReplaceLength));
135: }
136:
137: int start = 0;
138: int end = aSource.indexOf(aFind, start);
139: if (end == -1) {
140: // nothing was found in the string to replace.
141: // we can get this if the find and replace strings
142: // are the same length because we didn't check before.
143: // in this case, we will return the original string
144: return aSource;
145: }
146: // it looks like we actually have something to replace
147: // *sigh* allocate memory for it.
148: StringBuffer sb = new StringBuffer(length);
149:
150: // Scan s and do the replacements
151: if (end != -1) {
152: sb.append(aSource.substring(start, end));
153: sb.append(aReplace);
154: start = end + lFindLength;
155: end = aSource.indexOf(aFind, start);
156: }
157: end = lSourceLength;
158: sb.append(aSource.substring(start, end));
159:
160: return (sb.toString());
161: }
162:
163: public static void main(String[] args) {
164: String configList = "http=www.google.com:80;https=www.s.s.c;gopher=ss.w.s.c";
165:
166: String out = StringUtil.replaceAll(configList, "=", "://");
167: System.out.println(out);
168: out = StringUtil.replaceFirst(configList, "=", "://");
169: System.out.println(out);
170:
171: }
172: }
|