package com.sun.portal.proxylet.client.common;
public class StringUtil
{
public static String replaceAll( final String aSource,
final String aFind,
String aReplace )
{
int lFindLength;
// the next statement has the side effect of throwing a null pointer
// exception if s is null.
if ( aSource == null ||
aFind == null ||
( lFindLength = aFind.length() ) == 0 )
{
// If there is nothing to find, we won't try and find it.
return aSource;
}
if ( aReplace == null )
{
// a null string and an empty string are the same
// for replacement purposes.
aReplace = "";
}
int lSourceLength = aSource.length();
int lReplaceLength = aReplace.length();
// We need to figure out how long our resulting string will be.
// This is required because without it, the possible resizing
// and copying of memory structures could lead to an unacceptable runtime.
// In the worst case it would have to be resized n times with each
// resize having a O(n) copy leading to an O(n^2) algorithm.
int length;
if ( lFindLength == lReplaceLength )
{
// special case in which we don't need to count the replacements
// because the count falls out of the length formula.
length = lSourceLength;
}
else
{
int count;
int start;
int end;
// Scan s and count the number of times we find our target.
count = 0;
start = 0;
while ( ( end = aSource.indexOf( aFind, start ) ) != -1 )
{
count++;
start = end + lFindLength;
}
if ( count == 0 )
{
// special case in which on first pass, we find there is nothing
// to be replaced. No need to do a second pass or create a string buffer.
return aSource;
}
length = lSourceLength - ( count * ( lFindLength - lReplaceLength ) );
}
int start = 0;
int end = aSource.indexOf( aFind, start );
if ( end == -1 )
{
// nothing was found in the string to replace.
// we can get this if the find and replace strings
// are the same length because we didn't check before.
// in this case, we will return the original string
return aSource;
}
// it looks like we actually have something to replace
// *sigh* allocate memory for it.
StringBuffer sb = new StringBuffer( length );
// Scan s and do the replacements
while ( end != -1 )
{
sb.append( aSource.substring( start, end ) );
sb.append( aReplace );
start = end + lFindLength;
end = aSource.indexOf( aFind, start );
}
end = lSourceLength;
sb.append( aSource.substring( start, end ) );
return ( sb.toString() );
}
public static String replaceFirst( final String aSource,
final String aFind,
String aReplace )
{
int lFindLength;
// the next statement has the side effect of throwing a null pointer
// exception if s is null.
if ( aSource == null ||
aFind == null ||
( lFindLength = aFind.length() ) == 0 )
{
// If there is nothing to find, we won't try and find it.
return aSource;
}
if ( aReplace == null )
{
// a null string and an empty string are the same
// for replacement purposes.
aReplace = "";
}
int lSourceLength = aSource.length();
int lReplaceLength = aReplace.length();
// We need to figure out how long our resulting string will be.
// This is required because without it, the possible resizing
// and copying of memory structures could lead to an unacceptable runtime.
// In the worst case it would have to be resized n times with each
// resize having a O(n) copy leading to an O(n^2) algorithm.
int length;
if ( lFindLength == lReplaceLength )
{
// special case in which we don't need to count the replacements
// because the count falls out of the length formula.
length = lSourceLength;
}
else
{
int count;
int start;
int end;
// Scan s and count the number of times we find our target.
count = 0;
start = 0;
while ( ( end = aSource.indexOf( aFind, start ) ) != -1 )
{
count++;
start = end + lFindLength;
}
if ( count == 0 )
{
// special case in which on first pass, we find there is nothing
// to be replaced. No need to do a second pass or create a string buffer.
return aSource;
}
length = lSourceLength - ( count * ( lFindLength - lReplaceLength ) );
}
int start = 0;
int end = aSource.indexOf( aFind, start );
if ( end == -1 )
{
// nothing was found in the string to replace.
// we can get this if the find and replace strings
// are the same length because we didn't check before.
// in this case, we will return the original string
return aSource;
}
// it looks like we actually have something to replace
// *sigh* allocate memory for it.
StringBuffer sb = new StringBuffer( length );
// Scan s and do the replacements
if ( end != -1 )
{
sb.append( aSource.substring( start, end ) );
sb.append( aReplace );
start = end + lFindLength;
end = aSource.indexOf( aFind, start );
}
end = lSourceLength;
sb.append( aSource.substring( start, end ) );
return ( sb.toString() );
}
public static void main(String[] args)
{
String configList = "http=www.google.com:80;https=www.s.s.c;gopher=ss.w.s.c";
String out = StringUtil.replaceAll(configList,"=", "://");
System.out.println(out);
out = StringUtil.replaceFirst(configList,"=", "://");
System.out.println(out);
}
}
|