org.qedeq.base.utility
Class StringUtility

java.lang.Object
  extended by org.qedeq.base.utility.StringUtility

public final class StringUtility
extends java.lang.Object

A collection of useful static methods for strings. LATER mime 20070101: use StringBuilder instead of StringBuffer if working under JDK 1.5

Author:
Michael Meyling

Method Summary
static java.lang.String alignRight(long number, int length)
          Trim an integer with leading spaces to a given maximum length.
static java.lang.String alignRight(java.lang.String string, int length)
          Trim a String with leading spaces to a given maximum length.
static java.lang.String asLines(java.util.Set set)
          Evaluates toString at the elements of a Set and returns them as line separated Strings.
static java.lang.String byte2Hex(byte[] data)
          Get a hex string representation for an byte array.
static void deleteLineLeadingWhitespace(java.lang.StringBuffer buffer)
          Search for first line followed by whitespace and delete this string within the whole text.
static java.lang.String escapeProperty(java.lang.String value)
          Return a String like it appears in an property file.
static java.lang.String escapeXml(java.lang.String value)
          Escapes the characters in a String using XML entities.
static java.lang.String format(long number, int length)
          Trim an integer with leading zeros to a given maximum length.
static java.lang.String getClassName(java.lang.Class clazz)
          Get non qualified class name.
static java.lang.String getLastDotString(java.lang.String full)
          Get last dot separated string part.
static java.lang.String getLastTwoDotStrings(java.lang.String full)
          Get last two dot separated string part.
static java.lang.StringBuffer getSpaces(int length)
          Get amount of spaces.
static java.lang.String getSystemLineSeparator()
          Get platform dependent line separator.
static byte[] hex2byte(java.lang.String hex)
          Get a byte array of a hex string representation.
static java.lang.String hex2String(java.lang.String hex)
          Get a String out of a hex string representation.
static java.lang.String hex2String(java.lang.String hex, java.lang.String encoding)
          Get a String out of a hex string representation.
static boolean isIn(java.lang.String lookFor, java.lang.String[] array)
          Does a given string is an element of a given string array?
static boolean isLetterDigitString(java.lang.String text)
          Tests if given String begins with a letter and contains only letters and digits.
static boolean isNotIn(java.lang.String lookFor, java.lang.String[] array)
          Does a given string is not an element of a given string array?
static java.lang.String quote(java.lang.String unquoted)
          Quotes a String.
static void replace(java.lang.StringBuffer text, java.lang.String search, java.lang.String replace)
          Replaces all occurrences of search in text by replace.
static java.lang.String replace(java.lang.String text, java.lang.String search, java.lang.String replace)
          Replaces all occurrences of search in text by replace and returns the result.
static java.lang.String[] split(java.lang.String text, java.lang.String delimiter)
          Split String by given delimiter.
static java.lang.String string2Hex(java.lang.String data)
          Get a hex string representation for a String.
static java.lang.String string2Hex(java.lang.String data, java.lang.String encoding)
          Get a hex string representation for a String.
static java.lang.String substring(java.lang.String text, int position, int length)
          Return substring of text.
static java.lang.String toString(java.util.Map map)
          Returns a readable presentation of a Map.
static java.lang.String toString(java.lang.Object[] list)
          Returns a readable presentation of an Object array.
static java.lang.String toString(java.util.Set set)
          Returns a readable presentation of a Set.
static java.lang.String unescapeXml(java.lang.String value)
          Unescapes a string containing XML entity escapes to a string containing the actual Unicode characters corresponding to the escapes.
static java.lang.String useSystemLineSeparator(java.lang.String text)
          Creates String with platform dependent line ends.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

replace

public static java.lang.String replace(java.lang.String text,
                                       java.lang.String search,
                                       java.lang.String replace)
Replaces all occurrences of search in text by replace and returns the result.

Parameters:
text - text to work on, can be null
search - replace this text by replace, can be null
replace - replacement for search, can be null
Returns:
resulting string (is never null)

replace

public static void replace(java.lang.StringBuffer text,
                           java.lang.String search,
                           java.lang.String replace)
Replaces all occurrences of search in text by replace.

Parameters:
text - Text to work on. Must not be null.
search - replace this text by replace. Can be null.
replace - replacement for search. Can be null.
Throws:
java.lang.NullPointerException - text is null.

substring

public static java.lang.String substring(java.lang.String text,
                                         int position,
                                         int length)
Return substring of text. Position might be negative if length is big enough. If the string limits are exceeded this method returns at least all characters within the boundaries. If no characters are within the given limits an empty string is returned.

Parameters:
text - Text to work on. Must not be null.
position - Starting position. Might be negative.
length - Maximum length to get.
Returns:
Substring of maximum length length and starting with position.
Throws:
java.lang.NullPointerException - text is null.

toString

public static java.lang.String toString(java.lang.Object[] list)
Returns a readable presentation of an Object array. Something like ("a", null, 13)" if we have the "a", null, 13. Objects of type CharSequence are quoted.

Parameters:
list - List of Objects.
Returns:
List notation.

toString

public static java.lang.String toString(java.util.Set set)
Returns a readable presentation of a Set. Something like {"a", null, "13} if we have "a", null, 13. Objects of type CharSequence are quoted.

Parameters:
set - Set of objects.
Returns:
Set notation for toString() results.

toString

public static java.lang.String toString(java.util.Map map)
Returns a readable presentation of a Map. Something like "{a=2, b=null, c=12}" if the Map contains (a, 2), (b, null), (c, 12).

Parameters:
map - Map of objects mappings.
Returns:
Set notation for toString() results.

asLines

public static java.lang.String asLines(java.util.Set set)
Evaluates toString at the elements of a Set and returns them as line separated Strings. After the last element no carriage return is inserted.

Parameters:
set - Set of objects.
Returns:
List of toString() results.

quote

public static java.lang.String quote(java.lang.String unquoted)
Quotes a String. A a quote character " is appended at the beginning and the end of the String. If a quote character occurs within the string it is replaced by two quotes.

Parameters:
unquoted - the unquoted String, must not be null
Returns:
quoted String
Throws:
java.lang.NullPointerException - if unquoted == null

isLetterDigitString

public static boolean isLetterDigitString(java.lang.String text)
Tests if given String begins with a letter and contains only letters and digits.

Parameters:
text - test this
Returns:
is text only made of letters and digits and has a leading letter?
Throws:
java.lang.NullPointerException - if text == null

getSpaces

public static java.lang.StringBuffer getSpaces(int length)
Get amount of spaces.

Parameters:
length - number of spaces
Returns:
String contains exactly number spaces

getLastDotString

public static java.lang.String getLastDotString(java.lang.String full)
Get last dot separated string part.

Parameters:
full - String with dots in it. Also null is accepted.
Returns:
All after the last dot in full. Is never null.

getLastTwoDotStrings

public static java.lang.String getLastTwoDotStrings(java.lang.String full)
Get last two dot separated string part.

Parameters:
full - String with dots in it. Also null is accepted.
Returns:
All after the next to last dot in full. Is never null.

getClassName

public static java.lang.String getClassName(java.lang.Class clazz)
Get non qualified class name.

Parameters:
clazz - Class.
Returns:
Non qualified class name.

deleteLineLeadingWhitespace

public static void deleteLineLeadingWhitespace(java.lang.StringBuffer buffer)
Search for first line followed by whitespace and delete this string within the whole text.

For example the following text

       Do you know the muffin man,
       The muffin man, the muffin man,
       Do you know the muffin man,
       Who lives on Drury Lane?
will be converted into:
Do you know the muffin man,
The muffin man, the muffin man,
Do you know the muffin man,
Who lives on Drury Lane?

Parameters:
buffer - Work on this text.

escapeProperty

public static java.lang.String escapeProperty(java.lang.String value)
Return a String like it appears in an property file. Thus certain characters are escaped.

Parameters:
value - Escape this value.
Returns:
Escaped form.

alignRight

public static final java.lang.String alignRight(long number,
                                                int length)
Trim an integer with leading spaces to a given maximum length. If length is shorter than the String representation of number no digit is cut. So the resulting String might be longer than length.

Parameters:
number - Format this long.
length - Maximum length. Must not be bigger than 20 and less than 1.
Returns:
String with minimum length, trimmed with leading spaces.

alignRight

public static final java.lang.String alignRight(java.lang.String string,
                                                int length)
Trim a String with leading spaces to a given maximum length.

Parameters:
string - Format this string.
length - Minimum length. Must not be bigger than 20 and less than 1.
Returns:
String with minimum length, trimmed with leading spaces.

format

public static final java.lang.String format(long number,
                                            int length)
Trim an integer with leading zeros to a given maximum length.

Parameters:
number - Format this long.
length - Maximum length. Must not be bigger than 20 and less than 1.
Returns:
String with minimum length, trimmed with leading spaces.

byte2Hex

public static java.lang.String byte2Hex(byte[] data)
Get a hex string representation for an byte array.

Parameters:
data - byte array to work on
Returns:
hex String of hex codes.

string2Hex

public static java.lang.String string2Hex(java.lang.String data)
Get a hex string representation for a String. Uses UTF-8 encoding.

Parameters:
data - String to work on
Returns:
hex String of hex codes.

string2Hex

public static java.lang.String string2Hex(java.lang.String data,
                                          java.lang.String encoding)
                                   throws java.io.UnsupportedEncodingException
Get a hex string representation for a String.

Parameters:
data - String to work on
encoding - Use this String encoding.
Returns:
hex String of hex codes.
Throws:
java.io.UnsupportedEncodingException - Encoding not supported.

hex2byte

public static byte[] hex2byte(java.lang.String hex)
Get a byte array of a hex string representation.

Parameters:
hex - Hex string representation of data.
Returns:
Data array.
Throws:
java.lang.IllegalArgumentException - Padding wrong or illegal hexadecimal character.

hex2String

public static java.lang.String hex2String(java.lang.String hex)
Get a String out of a hex string representation. Uses UTF-8 encoding.

Parameters:
hex - Hex string representation of data.
Returns:
Data as String.
Throws:
java.lang.IllegalArgumentException - Padding wrong or illegal hexadecimal character.

hex2String

public static java.lang.String hex2String(java.lang.String hex,
                                          java.lang.String encoding)
                                   throws java.io.UnsupportedEncodingException
Get a String out of a hex string representation.

Parameters:
hex - Hex string representation of data.
encoding - Use this String encoding.
Returns:
Data as String.
Throws:
java.io.UnsupportedEncodingException - Encoding not supported.
java.lang.IllegalArgumentException - Padding wrong or illegal hexadecimal character.

getSystemLineSeparator

public static java.lang.String getSystemLineSeparator()
Get platform dependent line separator. ("\n" on UNIX).

Returns:
Platform dependent line separator.

useSystemLineSeparator

public static java.lang.String useSystemLineSeparator(java.lang.String text)
Creates String with platform dependent line ends. If text is null or empty nothing is changed. At the end of the String the platform dependent line end is added whether or not the original text ends with such a sequence.

Parameters:
text - Text with CR or CR LF as line end markers. Might be null.
Returns:
Text with platform dependent line ends.

split

public static java.lang.String[] split(java.lang.String text,
                                       java.lang.String delimiter)
Split String by given delimiter. "a:b:c" is converted to "a", "b", "c". "a:b:c:" is converted to "a", "b", "c", "".

Parameters:
text - Text to split.
delimiter - Split at these points.
Returns:
Split text.

escapeXml

public static java.lang.String escapeXml(java.lang.String value)

Escapes the characters in a String using XML entities.

For example: "bread" & "butter" => "bread" & "butter".

Supports only the five basic XML entities (gt, lt, quot, amp, apos). Does not support DTDs or external entities.

Note that unicode characters greater than 0x7f are currently escaped to their numerical \\u equivalent. This may change in future releases.

Parameters:
value - The String to escape, may be null.
Returns:
A new escaped String, null if null string input
See Also:
unescapeXml(java.lang.String)

unescapeXml

public static java.lang.String unescapeXml(java.lang.String value)

Unescapes a string containing XML entity escapes to a string containing the actual Unicode characters corresponding to the escapes.

Supports only the five basic XML entities (gt, lt, quot, amp, apos). Does not support DTDs or external entities.

Note that numerical \\u unicode codes are unescaped to their respective unicode characters. This may change in future releases.

Parameters:
value - The String to unescape, may be null.
Returns:
A new unescaped String, null if null string input
See Also:
escapeXml(String)

isNotIn

public static boolean isNotIn(java.lang.String lookFor,
                              java.lang.String[] array)
Does a given string is not an element of a given string array?

Parameters:
lookFor - Look for this string.
array - The array we look through.
Returns:
Is the given string not an element of the array?

isIn

public static boolean isIn(java.lang.String lookFor,
                           java.lang.String[] array)
Does a given string is an element of a given string array?

Parameters:
lookFor - Look for this string.
array - The array we look through.
Returns:
Is the given string an element of the array?


Copyright © 2014. All Rights Reserved.