com.ora.jsp.util
Class StringFormat
java.lang.Object
|
+--com.ora.jsp.util.StringFormat
- public class StringFormat
- extends java.lang.Object
This class contains a number of static methods that can be used to
validate the format of Strings, typically received as input from
a user, and to format values as Strings that can be used in
HTML output without causing interpretation problems.
- Version:
- 1.0
- Author:
- Hans Bergsten, Gefion software
Field Summary |
private static java.text.SimpleDateFormat |
dateFormat
|
private static java.text.DecimalFormat |
numberFormat
|
Method Summary |
static boolean |
isValidDate(java.lang.String dateString,
java.lang.String dateFormatPattern)
Returns true if the specified date string represents a valid
date in the specified format. |
static boolean |
isValidEmailAddr(java.lang.String emailAddrString)
Returns true if the email string contains an at sign ("@") and
at least one dot ("."), i.e. |
static boolean |
isValidInteger(java.lang.String numberString,
int min,
int max)
Returns true if the specified number string represents a valid
integer in the specified range. |
static boolean |
isValidString(java.lang.String value,
java.lang.String[] validStrings,
boolean ignoreCase)
Returns true if the specified string matches a string in the set
of provided valid strings, ignoring case if specified. |
static java.lang.String |
replaceInString(java.lang.String in,
java.lang.String from,
java.lang.String to)
Replaces one string with another throughout a source string. |
static java.util.Date |
toDate(java.lang.String dateString,
java.lang.String dateFormatPattern)
Converts a String to a Date, using the specified pattern. |
static java.lang.String |
toHTMLString(java.lang.String in)
Returns the specified string converted to a format suitable for
HTML. |
static java.lang.Number |
toNumber(java.lang.String numString,
java.lang.String numFormatPattern)
Converts a String to a Number, using the specified pattern. |
Methods inherited from class java.lang.Object |
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait |
dateFormat
private static java.text.SimpleDateFormat dateFormat
numberFormat
private static java.text.DecimalFormat numberFormat
StringFormat
public StringFormat()
isValidDate
public static boolean isValidDate(java.lang.String dateString,
java.lang.String dateFormatPattern)
- Returns true if the specified date string represents a valid
date in the specified format.
- Parameters:
dateString
- a String representing a date/time.dateFormatPattern
- a String specifying the format to be used
when parsing the dateString. The pattern is expressed with the
pattern letters defined for the java.text.SimpleDateFormat class.- Returns:
- true if valid, false otherwise
isValidInteger
public static boolean isValidInteger(java.lang.String numberString,
int min,
int max)
- Returns true if the specified number string represents a valid
integer in the specified range.
- Parameters:
numberString
- a String representing an integermin
- the minimal value in the valid rangemin
- the maximal value in the valid range- Returns:
- true if valid, false otherwise
isValidEmailAddr
public static boolean isValidEmailAddr(java.lang.String emailAddrString)
- Returns true if the email string contains an at sign ("@") and
at least one dot ("."), i.e. "hans@gefionsoftware.com" is accepted
but "hans@gefionsoftware" is not. Note! This rule is not always
correct (e.g. on an intranet it may be okay with just a name) and
does not gurantee a valid Internet email address but it takes
care of the most obvious Internet mail address format errors.
- Parameters:
emailAddrString
- a String representing an email address- Returns:
- true if valid, false otherwise
isValidString
public static boolean isValidString(java.lang.String value,
java.lang.String[] validStrings,
boolean ignoreCase)
- Returns true if the specified string matches a string in the set
of provided valid strings, ignoring case if specified.
- Parameters:
value
- the String validatevalidStrings
- an array of valid StringsignoreCase
- if true, case is ignored when comparing the value
to the set of validStrings- Returns:
- true if valid, false otherwise
toHTMLString
public static java.lang.String toHTMLString(java.lang.String in)
- Returns the specified string converted to a format suitable for
HTML. All signle-quote, double-quote, greater-than, less-than and
ampersand characters are replaces with their corresponding HTML
Character Entity code.
- Parameters:
in
- the String to convert- Returns:
- the converted String
toDate
public static java.util.Date toDate(java.lang.String dateString,
java.lang.String dateFormatPattern)
throws java.text.ParseException
- Converts a String to a Date, using the specified pattern.
(see java.text.SimpleDateFormat for pattern description)
- Parameters:
dateString
- the String to convertdateFormatPattern
- the pattern- Returns:
- the corresponding Date
- Throws:
ParseException,
- if the String doesn't match the pattern
toNumber
public static java.lang.Number toNumber(java.lang.String numString,
java.lang.String numFormatPattern)
throws java.text.ParseException
- Converts a String to a Number, using the specified pattern.
(see java.text.NumberFormat for pattern description)
- Parameters:
numString
- the String to convertnumFormatPattern
- the pattern- Returns:
- the corresponding Number
- Throws:
ParseException,
- if the String doesn't match the pattern
replaceInString
public static java.lang.String replaceInString(java.lang.String in,
java.lang.String from,
java.lang.String to)
- Replaces one string with another throughout a source string.
- Parameters:
in
- the source Stringfrom
- the sub String to replaceto
- the sub String to replace with- Returns:
- a new String with all occurences of from replaced by to