📄 utilstring.java
字号:
/*
* This class provides string manipulation methods.
*
* $Author: davis $
* $Date: 2004/12/18 21:23:31 $
* $Revision: 1.1 $
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Revision History:
*
* Written by Davis Swan in February, 2004.
*/
package com.sqlmagic.tinysql;
import java.text.*;
import java.util.*;
import java.lang.*;
import java.sql.Types;
public class UtilString
{
/*
* Is this a quoted string?
*/
public static boolean isQuotedString(String inputString)
{
String trimString;
int trimLength;
if ( inputString == (String)null ) return false;
trimString = inputString.trim();
trimLength = trimString.length();
if ( trimString.length() == 0 ) return false;
if ( ( trimString.charAt(0) == '\'' &
trimString.charAt(trimLength - 1) == '\'' ) |
( trimString.charAt(0) == '"' &
trimString.charAt(trimLength - 1) == '"' ) )
{
return true;
}
return false;
}
/*
* Remove enclosing quotes from a string.
*/
public static String removeQuotes(String inputString)
{
String trimString;
int trimLength;
if ( inputString == (String)null ) return inputString;
trimString = inputString.trim();
trimLength = trimString.length();
if ( trimString.length() == 0 ) return inputString;
if ( ( trimString.charAt(0) == '\'' &
trimString.charAt(trimLength - 1) == '\'' ) |
( trimString.charAt(0) == '"' &
trimString.charAt(trimLength - 1) == '"' ) )
{
return trimString.substring(1,trimString.length() - 1);
}
return inputString;
}
/*
* Convert a string to a double or return a default value.
*/
public static double doubleValue(String inputString)
{
return doubleValue(inputString,Double.MIN_VALUE);
}
public static double doubleValue(String inputString,double defaultValue )
{
try
{
return Double.parseDouble(inputString);
} catch (Exception e) {
return defaultValue;
}
}
/*
* Convert a date string in the format YYYYMMDD to the standard
* date output YYYY-MM-DD
*/
public static String toStandardDate(String inputDateString)
throws tinySQLException
{
String dateString,stdDateString;
int month;
if ( inputDateString == (String)null )
throw new tinySQLException ("Cannot format NULL date");
if ( inputDateString.length() < 8 )
throw new tinySQLException("Date " + inputDateString
+ " not in YYYYMMDD format");
/*
* Convert the input to YYYYMMDD - this is required because
* versions of tinySQL before 2.26d incorrectly stored dates in several
* different character string representations.
*/
dateString = dateValue(inputDateString);
stdDateString = dateString.substring(0,4) + "-"
+ dateString.substring(4,6) + "-"
+ dateString.substring(6,8);
return stdDateString;
}
/*
* Convert a date string in the format DD-MON-YY, DD-MON-YYYY, or YYYYMMDD
* to the output YYYYMMDD after checking the validity of all subfields.
* A tinySQLException is thrown if there are problems.
*/
public static String dateValue(String inputString ) throws tinySQLException
{
String months = "-JAN-FEB-MAR-APR-MAY-JUN-JUL-AUG-SEP-OCT-NOV-DEC-";
int[] daysInMonth = {31,29,31,30,31,30,31,31,30,31,30,31};
String dateString,dayField,monthName,monthField,yearField;
String[] ftFields;
FieldTokenizer ft;
int year,month,day,monthAt;
dateString = inputString.toUpperCase().trim();
if ( dateString.length() < 8 )
throw new tinySQLException(dateString + " is less than 8 characters.");
/*
* Check for YYYY-MM-DD format - convert to YYYYMMDD if found.
*/
if ( dateString.length() == 10 & dateString.charAt(4) == '-' &
dateString.charAt(7) == '-' )
{
dateString = dateString.substring(0,4) + dateString.substring(5,7)
+ dateString.substring(8,10);
}
/*
* First check for an 8 character field properly formatted.
*/
if ( dateString.length() == 8 & isInteger(dateString) )
{
try
{
year = Integer.parseInt(dateString.substring(0,4));
if ( year < 0 | year > 2100 )
throw new tinySQLException(dateString + " year not "
+ "recognized.");
month = Integer.parseInt(dateString.substring(4,6));
if ( month < 1 | month > 12 )
throw new tinySQLException(dateString + " month not "
+ "recognized.");
day = Integer.parseInt(dateString.substring(6,8));
if ( day < 1 | day > daysInMonth[month-1] )
throw new tinySQLException(dateString + " day not "
+ "recognized.");
return dateString;
} catch ( Exception dateEx ) {
throw new tinySQLException( dateEx.getMessage());
}
}
/*
* Check for dd-MON-YY formats - strip off TO_DATE if it exists.
*/
if ( dateString.startsWith("TO_DATE") )
{
dateString = dateString.substring(8,dateString.length()-1);
dateString = removeQuotes(dateString);
}
ft = new FieldTokenizer(dateString,'-',false);
ftFields = ft.getFields();
if ( ftFields.length < 3 )
{
throw new tinySQLException(dateString + " is not a date with "
+ "format DD-MON-YY!");
} else {
try
{
day = Integer.parseInt(ftFields[0]);
monthName = ftFields[1];
monthAt = months.indexOf("-" + monthName + "-");
if ( monthAt == -1 )
throw new tinySQLException(dateString + " month not "
+ "recognized.");
month = (monthAt + 4)/4;
if ( day < 1 | day > daysInMonth[month-1] )
throw new tinySQLException(dateString + " day not "
+ "between 1 and " + daysInMonth[month-1]);
year = Integer.parseInt(ftFields[2]);
if ( year < 0 | year > 2100 )
throw new tinySQLException(dateString + " year not "
+ "recognized.");
/*
* Assume that years < 50 are in the 21st century, otherwise
* the 20th.
*/
if ( year < 50 ) year = 2000 + year;
else year = 1900 + year;
dayField = Integer.toString(day);
if ( dayField.length() < 2 ) dayField = "0" + dayField;
monthField = Integer.toString(month);
if ( monthField.length() < 2 ) monthField = "0" + monthField;
yearField = Integer.toString(year);
return yearField + monthField + dayField;
} catch (Exception dayEx ) {
throw new tinySQLException(dateString + " exception "
+ dayEx.getMessage());
}
}
}
/*
* The following method replaces all occurrences of oldString with newString
* in the inputString. This function can be replaced with the native
* String method replaceAll in JDK 1.4 and above but is provide to support
* earlier versions of the JRE.
*/
public static String replaceAll(String inputString,String oldString,
String newString)
{
StringBuffer outputString = new StringBuffer(100);
int startIndex=0,nextIndex;
while ( inputString.substring(startIndex).indexOf(oldString) > -1 )
{
nextIndex = startIndex + inputString.substring(startIndex).indexOf(oldString);
if ( nextIndex > startIndex )
{
outputString.append(inputString.substring(startIndex,nextIndex));
}
outputString.append(newString);
startIndex = nextIndex + oldString.length();
}
if ( startIndex <= inputString.length() - 1 )
{
outputString.append(inputString.substring(startIndex));
}
return outputString.toString();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -