net.sf.jpkgmk.util
Class StringUtil

java.lang.Object
  extended by net.sf.jpkgmk.util.StringUtil

public class StringUtil
extends java.lang.Object

Since:
1.0
Version:
$Revision: 2 $ $Date: 2008-08-20 21:14:19 +0200 (Mi, 20 Aug 2008) $
Author:
gommma (gommma AT users.sourceforge.net), Last changed by: $Author: gommma $

Nested Class Summary
static class StringUtil.KeyValuePair
          Simple object associating a key with a value
static class StringUtil.RemoveResult
           
static class StringUtil.TruncationMode
          Enumeration of supported truncation modes.
 
Field Summary
static java.lang.String DEFAULT_SYSTEM_CHARSET
          Default encoding/charset
static java.lang.String UTF8_ENCODING
          The string literal for the UTF-8 character set
 
Method Summary
static int compare(java.lang.String first, java.lang.String second)
           
static java.lang.String createRandomStringWithBytes(int byteSize)
          Creates a random string with the given length
static java.lang.String createString(java.lang.String[] array, char delimiter)
          Converts the given array of strings into a single string using the given delimiter as separator.
static java.lang.String createString(java.lang.String[] array, int startIndex, char delimiter)
          Converts the given array of strings into a single string using the given delimiter as separator.
static java.lang.String getHexString(byte[] raw)
           
static int getLengthInBytes(java.lang.String text, java.lang.String charset)
          Returns the length of the byte array for the specified text.
static boolean isAlphaNumeric(java.lang.String stringToCheck)
          Checks if the given string is alphanumeric or not
static boolean isInteger(java.lang.String value)
           
static boolean isNullOrEmpty(java.lang.String string)
           
static StringUtil.RemoveResult removePrefix(java.lang.String string, java.lang.String prefix)
           
static java.lang.String replace(java.lang.String path, java.lang.String search, java.lang.String replacement)
          Replaces the first occurrence of the given 'search' string by the 'replacement' string.
static StringUtil.KeyValuePair resolveKeyValue(java.lang.String someString)
          Resolves a key value pair from the given string by splitting it at the first found '=' character.
static java.lang.String truncateToMaxLength(java.lang.String inputString, int maxLength)
          Truncates the given string if it is longer than the maximum length specified.
static java.lang.String truncateUtf8String(java.lang.String text, int maxLengthInBytes, StringUtil.TruncationMode mode)
          Returns the specified string truncated to the specified maximum length in bytes (when encoded in UTF-8 format), with an ellipses (...) if need be.
static void validateMode(java.lang.String mode)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UTF8_ENCODING

public static final java.lang.String UTF8_ENCODING
The string literal for the UTF-8 character set

See Also:
Constant Field Values

DEFAULT_SYSTEM_CHARSET

public static final java.lang.String DEFAULT_SYSTEM_CHARSET
Default encoding/charset

Method Detail

getHexString

public static java.lang.String getHexString(byte[] raw)
                                     throws java.io.UnsupportedEncodingException
Throws:
java.io.UnsupportedEncodingException

isNullOrEmpty

public static final boolean isNullOrEmpty(java.lang.String string)
Parameters:
string -
Returns:
Returns true if the given string is null or an empty string

replace

public static final java.lang.String replace(java.lang.String path,
                                             java.lang.String search,
                                             java.lang.String replacement)
Replaces the first occurrence of the given 'search' string by the 'replacement' string.

Parameters:
path - The path on which the replacement should be done
search - search string
replacement - replacement string
Returns:

createRandomStringWithBytes

public static java.lang.String createRandomStringWithBytes(int byteSize)
Creates a random string with the given length

Parameters:
byteSize -
Returns:

isInteger

public static boolean isInteger(java.lang.String value)

createString

public static java.lang.String createString(java.lang.String[] array,
                                            char delimiter)
Converts the given array of strings into a single string using the given delimiter as separator.

Parameters:
array -
delimiter -
Returns:

createString

public static java.lang.String createString(java.lang.String[] array,
                                            int startIndex,
                                            char delimiter)
Converts the given array of strings into a single string using the given delimiter as separator.

Parameters:
array -
startIndex -
delimiter -
Returns:

truncateToMaxLength

public static java.lang.String truncateToMaxLength(java.lang.String inputString,
                                                   int maxLength)
Truncates the given string if it is longer than the maximum length specified. The symbols '...' replace the last three characters to indicate that truncation occurred. If the string's length is less than the specified maximum, it is returned unchanged. If the given string is null, null is returned. Maximum lengths of less than 1 will be ignored.

Parameters:
inputString - The string to truncate, if necessary.
maxLength - The maximum length of the string, after truncation.
Returns:
A string at most maxLength characters long taken from the input string, or the input string if it is shorter than the specified maximum length.

truncateUtf8String

public static java.lang.String truncateUtf8String(java.lang.String text,
                                                  int maxLengthInBytes,
                                                  StringUtil.TruncationMode mode)
Returns the specified string truncated to the specified maximum length in bytes (when encoded in UTF-8 format), with an ellipses (...) if need be.

Parameters:
text - The text to truncate.
maxLengthInBytes - The maximum length in bytes.
mode - The mode for executing the truncation. This can be one of If the specified mode is any unknown mode, the default mode StringUtil.TruncationMode.END is used.
Returns:
The specified string truncated to the specified maximum length in bytes.
Throws:
java.lang.IllegalArgumentException - If maxLengthInBytes is less than 3 or if the given StringUtil.TruncationMode is unknown.

getLengthInBytes

public static int getLengthInBytes(java.lang.String text,
                                   java.lang.String charset)
Returns the length of the byte array for the specified text.

Parameters:
text - The text whose length in bytes should be computed.
charset - The encoding of the text which is needed for converting the text to a byte array. For available encodings, refer to Charset.availableCharsets().
Returns:
The length of the byte array for the text in the specified encoding. If the specified charset is not supported, this method returns zero.
Throws:
java.lang.NullPointerException - If the specified string is null.

resolveKeyValue

public static StringUtil.KeyValuePair resolveKeyValue(java.lang.String someString)
Resolves a key value pair from the given string by splitting it at the first found '=' character.

Parameters:
someString - String to resolve. If the string is null or empty, a runtime exception is thrown
Returns:

isAlphaNumeric

public static boolean isAlphaNumeric(java.lang.String stringToCheck)
Checks if the given string is alphanumeric or not

Parameters:
stringToCheck -
Returns:

compare

public static int compare(java.lang.String first,
                          java.lang.String second)

validateMode

public static void validateMode(java.lang.String mode)

removePrefix

public static StringUtil.RemoveResult removePrefix(java.lang.String string,
                                                   java.lang.String prefix)


Copyright © 2007-2008. All Rights Reserved.