org.ceryle.util
Class TextUtil

java.lang.Object
  extended by org.ceryle.util.TextUtil

public class TextUtil
extends Object

Provides some general text search and replace utilities. All methods are static.

Since:
JDK1.3
Version:
$Id: TextUtil.java,v 3.2 2007-06-15 12:09:57 altheim Exp $
Author:
Murray Altheim

Constructor Summary
TextUtil()
           
 
Method Summary
static boolean characterMatches(boolean ignoreCase, char c1, char c2)
          Returns true if the character c1 matches character c2, ignoring case (as much as is provided by Java) if the boolean ignoreCase is true.
static String gsub(String src, String srch, String repl, boolean ignoreCase)
          Replace all (non-overlapping) occurrences of a String srch in String src by replacement repl.
static int indexOf(boolean ignoreCase, String source, String substring, int fromIndex)
          Returns the index within String source of the first occurrence of the specified substring, starting at the specified index.
static int indexOfIgnoreCase(String str, String substring)
          Returns the index within String str of the first occurrence of the specified substring, ignoring case.
static int indexOfIgnoreCase(String str, String substring, int fromIndex)
          Returns the index within String str of the first occurrence of the specified substring, ignoring case, and starting at the specified index.
static String oldGsub(String src, String srch, String repl, boolean ignoreCase)
          Replace all (non-overlapping) occurrences of a String srch in String src by replacement repl.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TextUtil

public TextUtil()
Method Detail

indexOfIgnoreCase

public static final int indexOfIgnoreCase(String str,
                                          String substring)
Returns the index within String str of the first occurrence of the specified substring, ignoring case. This is accomplished by uppercasing the entire source String, not very efficient for large Strings.

(This will likely be rewritten at some point with an actual character-by-character implementation; due to internationalization considerations this is quite a bit more difficult than a simple character match so we're currently just relying on Java's i18n features.)

Parameters:
str - the source String.
substring - the substring to search for.
Returns:
if the String argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.

indexOfIgnoreCase

public static final int indexOfIgnoreCase(String str,
                                          String substring,
                                          int fromIndex)
Returns the index within String str of the first occurrence of the specified substring, ignoring case, and starting at the specified index. This is accomplished by uppercasing the entire source String, not very efficient for large Strings.

Parameters:
str - any String.
substring - the substring to search for.
fromIndex - the index to start the search from.
Returns:
If the String argument occurs as a substring within this object at a starting index no smaller than fromIndex, then the index of the first character of the first such substring is returned. If it does not occur as a substring starting at fromIndex or beyond, -1 is returned.

indexOf

public static int indexOf(boolean ignoreCase,
                          String source,
                          String substring,
                          int fromIndex)
Returns the index within String source of the first occurrence of the specified substring, starting at the specified index. If the boolean ignoreCase is false, this performs the same as the java.lang.String method of similar ilk. This method is suitable for large source strings.

Also, as with java.lang.String's indexOf() method, there is no restriction on the value of fromIndex: if it exceeds either start or end boundary, it is set to that boundary.

Parameters:
source - the String to search within.
substring - the substring to search for.
fromIndex - the index to start the search from.
Returns:
the index of the first matching character of the substring within the source String, or -1 if no match.
Throws:
NullPointerException - if source is null

characterMatches

public static boolean characterMatches(boolean ignoreCase,
                                       char c1,
                                       char c2)
Returns true if the character c1 matches character c2, ignoring case (as much as is provided by Java) if the boolean ignoreCase is true.


gsub

public static final String gsub(String src,
                                String srch,
                                String repl,
                                boolean ignoreCase)
Replace all (non-overlapping) occurrences of a String srch in String src by replacement repl. If the pattern isn't found, the String is returned unchanged. A null source String returns null.

Parameters:
src - any String.
srch - the String to search for.
repl - the replacement String.
ignoreCase - a boolean determining case sensitivity of matches.
Returns:
Returns the String with all instances of the search pattern replaced by the replacement String. If no occurrences of the search pattern are found, returns the String unchanged.

oldGsub

public static final String oldGsub(String src,
                                   String srch,
                                   String repl,
                                   boolean ignoreCase)
Replace all (non-overlapping) occurrences of a String srch in String src by replacement repl. If the pattern isn't found, the String is returned unchanged.

Parameters:
src - any String.
srch - the String to search for.
repl - the replacement String.
ignoreCase - a boolean determining case sensitivity of matches.
Returns:
Returns the String with all instances of the search pattern replaced by the replacement String. If no occurrences of the search pattern are found, returns the String unchanged.


The Ceryle Project. Copyright ©2001-2007 Murray Altheim, All Rights Reserved. See LICENSE included with distribution.