ilex.util.TextUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opendcs Show documentation
Show all versions of opendcs Show documentation
A collection of software for aggregatting and processing environmental data such as from NOAA GOES satellites.
The newest version!
/*
* $Id: TextUtil.java,v 1.5 2020/01/31 19:42:07 mmaloney Exp $
*
* $Source: /home/cvs/repo/opendcs/src/ilex/util/TextUtil.java,v $
*
* $State: Exp $
*
* $Log: TextUtil.java,v $
* Revision 1.5 2020/01/31 19:42:07 mmaloney
* Added intEqual method to compare Integer objects allowing for null.
*
* Revision 1.4 2019/12/11 14:31:52 mmaloney
* Added splitQuoted method.
*
* Revision 1.3 2019/06/10 19:35:05 mmaloney
* Added dateEqual method.
*
* Revision 1.2 2019/03/28 13:00:11 mmaloney
* Added strEqualNE - consider null string the same as blank string.
*
* Revision 1.1.1.1 2014/05/19 15:28:59 mmaloney
* OPENDCS 6.0 Initial Checkin
*
* Revision 1.4 2012/11/12 19:14:05 mmaloney
* CWMS uses 't' to mean true.
*
* Revision 1.3 2011/01/17 16:35:23 mmaloney
* Added getFirstLine method.
*
* Revision 1.2 2009/10/08 17:15:52 mjmaloney
* fixed comment
*
* Revision 1.1 2008/04/04 18:21:10 cvs
* Added legacy code to repository
*
* Revision 1.20 2007/09/29 21:58:41 mmaloney
* dev
*
* Revision 1.19 2007/05/25 14:07:13 mmaloney
* dev
*
* Revision 1.18 2006/12/23 18:16:05 mmaloney
* dev
*
* Revision 1.17 2004/08/30 14:50:32 mjmaloney
* Javadocs
*
* Revision 1.16 2004/06/21 13:31:53 mjmaloney
* Added scanAssign method.
*
* Revision 1.15 2004/05/21 18:28:04 mjmaloney
* Added startsWithIgnoreCase method.
*
* Revision 1.14 2004/04/02 18:58:17 mjmaloney
* Created.
*
* Revision 1.13 2003/12/15 15:21:14 mjmaloney
* Improvements to support LRGS Config Editor & EDL files.
*
* Revision 1.12 2003/11/15 20:36:43 mjmaloney
* Added compareIgnoreCase method that tolerates null arguments.
*
* Revision 1.11 2003/09/02 14:37:28 mjmaloney
* Added TeeLogger. Added more control on msg format to Logger.
* Added TextUtil.fixedLengthFields method.
*
* Revision 1.10 2002/10/29 00:57:13 mjmaloney
* Added right/left justify functions to TextUtil.
*
* Revision 1.9 2002/08/29 05:59:56 chris
* Added the split() method; also added some tests.
*
* Revision 1.8 2001/11/09 14:35:22 mike
* dev
*
* Revision 1.7 2001/10/05 17:49:59 mike
* Added HumanReadableFormatter
*
* Revision 1.6 2001/03/19 03:11:57 mike
* *** empty log message ***
*
* Revision 1.5 2000/12/31 14:14:20 mike
* Added containsNoWhiteSpace method.
*
* Revision 1.4 2000/12/29 02:50:05 mike
* dev
*
* Revision 1.3 2000/12/27 22:03:54 mike
* Added isAllWhiteSpace
*
* Revision 1.2 2000/12/24 02:41:07 mike
* dev
*
* Revision 1.1 2000/01/07 23:04:51 mike
* Created
*
*
*/
package ilex.util;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.text.*;
/**
* This class contains a set of static methods that supplement the methods
* in the java.text package.
*/
public class TextUtil
{
/**
* Skip white space in the string starting at the specified parse
* position. Leave 'pp' updated to the first non-whitespace character.
* @param s the input string
* @param pp the ParsePosition object
* @return true if a non-whitespace character is found, false if the
* end of the string is reached.
*/
public static boolean skipWhitespace( String s, ParsePosition pp )
{
int i = pp.getIndex();
for(; i < s.length() && Character.isWhitespace(s.charAt(i)); i++);
pp.setIndex(i);
return i < s.length() ? true : false;
}
/**
* Returns true if the passed string is empty or contains only white space.
* @param s the input string
* @return true if string is all whitespace
*/
public static boolean isAllWhitespace( String s )
{
for(int i = 0; i < s.length(); i++)
if (!Character.isWhitespace(s.charAt(i)))
return false;
return true;
}
/**
* Returns true if the passed string is non-blank and contains no white-
* space.
* This is appropriate for strings used as identifiers (e.g. filenames,
* variable-names etc.)
* @param s the input string
* @return true if string has no whitespace
*/
public static boolean containsNoWhitespace( String s )
{
int len = s.length();
if (len == 0)
return false; // empty string
for(int i = 0; i < len; i++)
if (Character.isWhitespace(s.charAt(i)))
return false;
return true;
}
/**
* Collapses contiguouse whitespace in the input string into a single
* space character in the output. This is useful for formatting paragraphs
* that were read from a file that may contain extraneous newlines and
* indentation, for example a long descripting field that was indented
* in an XML file.
* @param in the input string
* @return string with all whitespace collapsed to single space
*/
public static String collapseWhitespace( String in )
{
in = in.trim();
StringBuffer sb = new StringBuffer(in.length());
boolean ws = false;
for(int i=0; i= width)
return in;
int lpad = (width - inlen) / 2;
int rpad = width - inlen - lpad;
StringBuffer sb = new StringBuffer();
for(int i=0; i split("a:b:c", ':') yields "a", "b", "c"
* split("a:b:", ':') yields "a", "b", ""
* split(":b:c", ':') yields "", "b", "c"
* split("a::c", ':') yields "a", "", "c"
* split("", ':') yields ""
* @param s the input string
* @param c the delimiter
* @return results
*/
public static String[] split( String s, char c )
{
Vector v = new Vector();
int i = 0;
int next;
while ((next = s.indexOf(c, i)) != -1) {
v.add(s.substring(i, next));
i = next + 1;
}
v.add(s.substring(i));
return (String[]) v.toArray(new String[0]);
}
/**
* Compares Strings. Either can be null.
* If both are null, equality is true.
* @param s1 string 1
* @param s2 string 2
* @return true if equal
*/
public static boolean strEqual( String s1, String s2 )
{
// let's use logic!
return !(s1==null ^ s2==null) && (s1==null || s1.equals(s2));
/*
if (s1 == null)
{
if (s2 == null)
return true;
else
return false;
}
else // s1 != null
{
if (s2 == null)
return false;
else
return s1.equals(s2);
}
*/
}
public static boolean dateEqual(Date d1, Date d2)
{
if (d1 == null)
{
if (d2 == null)
return true;
else
return false;
}
else // d1 != null
{
if (d2 == null)
return false;
else
return d1.equals(d2);
}
}
public static boolean intEqual(Integer i1, Integer i2)
{
if (i1 == null)
{
if (i2 == null)
return true;
else
return false;
}
else // i1 != null
{
if (i2 == null)
return false;
else
return i1.equals(i2);
}
}
/**
* Compare strings, but consider null the same as an empty string.
* @param s1
* @param s2
* @return
*/
public static boolean strEqualNE(String s1, String s2)
{
if (s1 == null) s1 = "";
if (s2 == null) s2 = "";
return strEqual(s1, s2);
}
/**
* Compares strings for equality, allowing either string to be null.
* Returns true if both arguments are null, or neither is null and they
* match, ignoring case.
* @param s1 string 1
* @param s2 string 2
* @return true if equal
*/
public static boolean strEqualIgnoreCase( String s1, String s2 )
{
// let's use logic!
return !(s1==null ^ s2==null) &&
(s1==null || s1.equalsIgnoreCase(s2));
/*
if (s1 == null)
{
if (s2 == null)
return true;
else
return false;
}
else // s1 != null
{
if (s2 == null)
return false;
else
return s1.equalsIgnoreCase(s2);
}
*/
}
/**
* Compares strings, allowing either string to be null.
* Returns true if both arguments are null, or neither is null and they
* match, ignoring case. A null string is considered greater than a
* non-null string.
* @param s1 string 1
* @param s2 string 2
* @return 0 if equal
*/
public static int strCompareIgnoreCase( String s1, String s2 )
{
if (s1 == null)
{
if (s2 == null)
return 0;
else
return 1;
}
else // s1 != null
{
if (s2 == null)
return -1;
else
return s1.compareToIgnoreCase(s2);
}
}
/**
* Returns true if the first string starts with a copy of the second
* string, without regard to case.
* @param s the input string
* @param v the string to check for
* @return true if the first string starts with a copy of the second
*/
public static boolean startsWithIgnoreCase( String s, String v )
{
return s.length() >= v.length()
&& s.substring(0, v.length()).equalsIgnoreCase(v);
}
/**
* Returns true if the first string ends with a copy of the second
* string, without regard to case.
* @param s the input string
* @param v the string to check for
* @return true if the first string starts with a copy of the second
*/
public static boolean endsWithIgnoreCase( String s, String v )
{
int len = s.length();
int vlen = v.length();
return len >= vlen
&& s.substring(len - vlen).equalsIgnoreCase(v);
}
/**
* Sets the length of the string by padding with blanks on the right.
* If the passed string's length is greater than len, it is truncated.
* The return value is guaranteed to be a string of exactly 'len' characters.
* @param s the input string
* @param len the desired length
* @return padded string
*/
public static String setLengthLeftJustify( String s, int len )
{
if (s == null)
s = "";
StringBuffer sb = new StringBuffer(len);
int x;
for(x=0; x