SoftNet-Consult Java Utility Library

com.softnetConsult.utils.string
Class StringTools

java.lang.Object
  extended by com.softnetConsult.utils.string.StringTools

public final class StringTools
extends java.lang.Object

Defines static utility methods for string processing.

This product includes software developed by the SoftNet-Consult Java Utility Library project and its contributors.
(http://java-tools.sourceforge.net)
Copyright (c) 2007-2008 SoftNet-Consult.
Copyright (c) 2007-2008 G. Paperin.
All rights reserved.

File: StringTools.java
Library API version: "2.02"
Java compliance version: "1.5"

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following terms and conditions are met:

1. Redistributions of source code must retain the above acknowledgement of the SoftNet-Consult Java Utility Library project, the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above acknowledgement of the SoftNet-Consult Java Utility Library project, the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software or any derived software must display the following acknowledgement:
This product includes software developed by the SoftNet-Consult Java Utility Library project and its contributors.

THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Version:
"2.02"
Author:
Greg Paperin (http://www.paperin.org)

Field Summary
private static java.text.NumberFormat singeltonNumberFormatter
          An internal singelton instance of a default number formatter.
 
Constructor Summary
private StringTools()
          Prevents instances of this class from being created as this class contains only static utility methods.
 
Method Summary
static void enforceNumberFormatterReset()
          Resets the internal number formatter such that next time it is required (e.g.
static java.lang.String ensureMinLength(java.lang.String str, int length)
          Ensures that the specified string is not shorter than the specified length by prefixing it with a white spaces.
static java.lang.String ensureMinLength(java.lang.String str, int length, char fill)
          Ensures that the specified string is not shorter than the specified length by prefixing it with the specified character as necessary.
private static java.text.NumberFormat getSingeltonNumberFormatter()
          Used internally to access a singelton instance of a default number formatter.
static byte parseByte(java.lang.String str)
          Parses a string to a byte-number using the current default locale.
This is equivalent to parseNumber(str).byteValue().
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time.
static double parseDouble(java.lang.String str)
          Parses a string to a double-number using the current default locale.
This is equivalent to parseNumber(str).doubleValue().
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time.
static float parseFloat(java.lang.String str)
          Parses a string to a float-number using the current default locale.
This is equivalent to parseNumber(str).floatValue().
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time.
static int parseInt(java.lang.String str)
          Parses a string to a int-number using the current default locale.
This is equivalent to parseNumber(str).intValue().
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time.
static long parseLong(java.lang.String str)
          Parses a string to a long-number using the current default locale.
This is equivalent to parseNumber(str).longValue().
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time.
static java.lang.Number parseNumber(java.lang.String str)
          Parses a string to a number using the current default locale.
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time.
static short parseShort(java.lang.String str)
          Parses a string to a short-number using the current default locale.
This is equivalent to parseNumber(str).shortValue().
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time.
static java.lang.String toString(byte val)
          Formats a byte-value to a String using the current default locale.
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time.
static java.lang.String toString(double val)
          Formats a double-value to a String using the current default locale.
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time.
static java.lang.String toString(float val)
          Formats a float-value to a String using the current default locale.
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time.
static java.lang.String toString(int val)
          Formats a int-value to a String using the current default locale.
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time.
static java.lang.String toString(long val)
          Formats a long-value to a String using the current default locale.
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time.
static java.lang.String toString(short val)
          Formats a short-value to a String using the current default locale.
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

singeltonNumberFormatter

private static java.text.NumberFormat singeltonNumberFormatter
An internal singelton instance of a default number formatter.

Constructor Detail

StringTools

private StringTools()
Prevents instances of this class from being created as this class contains only static utility methods.

Method Detail

getSingeltonNumberFormatter

private static java.text.NumberFormat getSingeltonNumberFormatter()
Used internally to access a singelton instance of a default number formatter.

Returns:
A singelton instance of a default number formatter.

enforceNumberFormatterReset

public static void enforceNumberFormatterReset()
Resets the internal number formatter such that next time it is required (e.g. for one of this class' methods parseXXXX(String) and toString(xxxx)) a new formatter is re-created using the current default locale. This method may be useful after the system default locale has been changed by the user in order to update the state of this class' formatter.


ensureMinLength

public static java.lang.String ensureMinLength(java.lang.String str,
                                               int length)
Ensures that the specified string is not shorter than the specified length by prefixing it with a white spaces. This is equivalent to ensureMinLength(str, length, ' ').

Parameters:
str - A string (null-strings are converted to the string "null").
length - The required minimum length for the string.
Returns:
The string elongated by prefixing it with white spaces to ensure the specified minimum length.

ensureMinLength

public static java.lang.String ensureMinLength(java.lang.String str,
                                               int length,
                                               char fill)
Ensures that the specified string is not shorter than the specified length by prefixing it with the specified character as necessary.

Parameters:
str - A string (null-strings are converted to the string "null").
length - The required minimum length for the string.
fill - The character to use for the prefix.
Returns:
The string elongated by prefixing it with the specified character to ensure the specified minimum length.

parseNumber

public static java.lang.Number parseNumber(java.lang.String str)
                                    throws java.lang.NumberFormatException
Parses a string to a number using the current default locale.
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time. The same locale is then always reused, even if the default system locale is changed. In order to enforce the creation of a new formatter, use the method enforceNumberFormatterReset().

Parameters:
str - A string.
Returns:
The number represented by the specifed string.
Throws:
java.lang.NumberFormatException - If the specified string is null or if the beginning of the specified string cannot be parsed; in the latter case the thrown exception encapsulates the ParseException raised by the formatter as its cause.
See Also:
NumberFormat.parse(String)

parseByte

public static byte parseByte(java.lang.String str)
                      throws java.lang.NumberFormatException
Parses a string to a byte-number using the current default locale.
This is equivalent to parseNumber(str).byteValue().
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time. The same locale is then always reused, even if the default system locale is changed. In order to enforce the creation of a new formatter, use the method enforceNumberFormatterReset().

Parameters:
str - A string.
Returns:
The number represented by the specifed string.
Throws:
java.lang.NumberFormatException - If the specified string is null or if the beginning of the specified string cannot be parsed; in the latter case the thrown exception encapsulates the ParseException raised by the formatter as its cause.
See Also:
NumberFormat.parse(String)

parseShort

public static short parseShort(java.lang.String str)
                        throws java.lang.NumberFormatException
Parses a string to a short-number using the current default locale.
This is equivalent to parseNumber(str).shortValue().
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time. The same locale is then always reused, even if the default system locale is changed. In order to enforce the creation of a new formatter, use the method enforceNumberFormatterReset().

Parameters:
str - A string.
Returns:
The number represented by the specifed string.
Throws:
java.lang.NumberFormatException - If the specified string is null or if the beginning of the specified string cannot be parsed; in the latter case the thrown exception encapsulates the ParseException raised by the formatter as its cause.
See Also:
NumberFormat.parse(String)

parseInt

public static int parseInt(java.lang.String str)
                    throws java.lang.NumberFormatException
Parses a string to a int-number using the current default locale.
This is equivalent to parseNumber(str).intValue().
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time. The same locale is then always reused, even if the default system locale is changed. In order to enforce the creation of a new formatter, use the method enforceNumberFormatterReset().

Parameters:
str - A string.
Returns:
The number represented by the specifed string.
Throws:
java.lang.NumberFormatException - If the specified string is null or if the beginning of the specified string cannot be parsed; in the latter case the thrown exception encapsulates the ParseException raised by the formatter as its cause.
See Also:
NumberFormat.parse(String)

parseLong

public static long parseLong(java.lang.String str)
                      throws java.lang.NumberFormatException
Parses a string to a long-number using the current default locale.
This is equivalent to parseNumber(str).longValue().
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time. The same locale is then always reused, even if the default system locale is changed. In order to enforce the creation of a new formatter, use the method enforceNumberFormatterReset().

Parameters:
str - A string.
Returns:
The number represented by the specifed string.
Throws:
java.lang.NumberFormatException - If the specified string is null or if the beginning of the specified string cannot be parsed; in the latter case the thrown exception encapsulates the ParseException raised by the formatter as its cause.
See Also:
NumberFormat.parse(String)

parseFloat

public static float parseFloat(java.lang.String str)
                        throws java.lang.NumberFormatException
Parses a string to a float-number using the current default locale.
This is equivalent to parseNumber(str).floatValue().
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time. The same locale is then always reused, even if the default system locale is changed. In order to enforce the creation of a new formatter, use the method enforceNumberFormatterReset().

Parameters:
str - A string.
Returns:
The number represented by the specifed string.
Throws:
java.lang.NumberFormatException - If the specified string is null or if the beginning of the specified string cannot be parsed; in the latter case the thrown exception encapsulates the ParseException raised by the formatter as its cause.
See Also:
NumberFormat.parse(String)

parseDouble

public static double parseDouble(java.lang.String str)
                          throws java.lang.NumberFormatException
Parses a string to a double-number using the current default locale.
This is equivalent to parseNumber(str).doubleValue().
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time. The same locale is then always reused, even if the default system locale is changed. In order to enforce the creation of a new formatter, use the method enforceNumberFormatterReset().

Parameters:
str - A string.
Returns:
The number represented by the specifed string.
Throws:
java.lang.NumberFormatException - If the specified string is null or if the beginning of the specified string cannot be parsed; in the latter case the thrown exception encapsulates the ParseException raised by the formatter as its cause.
See Also:
NumberFormat.parse(String)

toString

public static java.lang.String toString(byte val)
Formats a byte-value to a String using the current default locale.
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time. The same locale is then always reused, even if the default system locale is changed. In order to enforce the creation of a new formatter, use the method enforceNumberFormatterReset().

Parameters:
val - A value to be formatted as a string.
Returns:
A string representing the specifed value according to the current default locale.

toString

public static java.lang.String toString(short val)
Formats a short-value to a String using the current default locale.
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time. The same locale is then always reused, even if the default system locale is changed. In order to enforce the creation of a new formatter, use the method enforceNumberFormatterReset().

Parameters:
val - A value to be formatted as a string.
Returns:
A string representing the specifed value according to the current default locale.

toString

public static java.lang.String toString(int val)
Formats a int-value to a String using the current default locale.
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time. The same locale is then always reused, even if the default system locale is changed. In order to enforce the creation of a new formatter, use the method enforceNumberFormatterReset().

Parameters:
val - A value to be formatted as a string.
Returns:
A string representing the specifed value according to the current default locale.

toString

public static java.lang.String toString(long val)
Formats a long-value to a String using the current default locale.
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time. The same locale is then always reused, even if the default system locale is changed. In order to enforce the creation of a new formatter, use the method enforceNumberFormatterReset().

Parameters:
val - A value to be formatted as a string.
Returns:
A string representing the specifed value according to the current default locale.

toString

public static java.lang.String toString(float val)
Formats a float-value to a String using the current default locale.
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time. The same locale is then always reused, even if the default system locale is changed. In order to enforce the creation of a new formatter, use the method enforceNumberFormatterReset().

Parameters:
val - A value to be formatted as a string.
Returns:
A string representing the specifed value according to the current default locale.

toString

public static java.lang.String toString(double val)
Formats a double-value to a String using the current default locale.
Note that the current locale is determined only once at the time when any of this class' methods parseXXXX(String) or toString(xxxx) is called for the first time. The same locale is then always reused, even if the default system locale is changed. In order to enforce the creation of a new formatter, use the method enforceNumberFormatterReset().

Parameters:
val - A value to be formatted as a string.
Returns:
A string representing the specifed value according to the current default locale.

SoftNet-Consult Java Utility Library is a member of SourceForge.net