com.levigo.util.base
Class Strings

java.lang.Object
  extended by com.levigo.util.base.Strings

public class Strings
extends Object

Utility methods for String processing.

Author:
Francois Fernandes

Method Summary
static byte[] asciiBytes(String s)
          Create a byte[] representation of the given string, using ASCII encoding.
static String asciiString(byte[] bytes)
          Construct a new String from the given input bytes, using ASCII encoding.
static void assertNotEmpty(String name, String value)
          Checks whether the given value is an empty String
static String create(InputStream is)
          Create a string by fully reading the supplied input stream using the default platform encoding.
static String create(InputStream is, String encoding)
          Create a string by fully reading the supplied input stream using the specified encoding.
static boolean empty(String s)
          Check whether the given String is empty or not.
static boolean emptyTrim(String s)
          Check whether the given String is empty or not.
static int indexOf(byte[] buffer, String matchString, int fromIndex)
          Finds the first occurrance of the given String in the given buffer.
static int lastIndexOf(byte[] buffer, String matchString, int toIndex)
          Finds the last occurrance of the given String in the given buffer.
static
<T> T
parsePrimitive(Class<T> targetType, String valueString)
          Parses the valueString based on given targetType and returns a value object decoded from given valueString.
static String replaceAll(String source, String pattern, String replacement)
          Replace a String inside of a String.
static String toFixedLength(String s, int length, char padding)
          Format the string into a fixed-length representation by either trimming it to the required size (if it is longer) or padding it at the end with the given padding character.
static String toHex(byte[] data)
          Convert byte array to hex string.
static String toHex(byte[] data, int offset, int len)
          Convert byte array to hex string.
static String trimCharacter(String stringToTrim, char charToRemove)
          Returns a copy of the string, with leading and trailing given parameter character omitted.
static String trimLeadingCharacter(String stringToTrim, char charToRemove)
          Returns a copy of the string, with leading given parameter character omitted
static String trimTrailingCharacter(String stringToTrim, char charToRemove)
          Returns a copy of the string, with trailing given parameter character omitted.
static byte[] utf16Bytes(String s)
           
static byte[] utf8Bytes(String s)
          Create a byte[] representation of the given string, using UTF-8 encoding.
static String utf8String(byte[] bytes)
          Construct a new String from the given input bytes, using UTF-8 encoding.
static boolean whitespace(String s)
          Check whether the given String only contains whitespace characters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

empty

public static boolean empty(String s)
Check whether the given String is empty or not. This method is null-safe. null will be interpreted as an empty String

Parameters:
s - the String to be checked.
Returns:
true if the String is either null or has zero length

emptyTrim

public static boolean emptyTrim(String s)
Check whether the given String is empty or not. This method basically works like empty(String) but with the addition that the String will be trimmed and checked if it contains any non whitespace character.

Parameters:
s - the String to be checked.
Returns:
true if the String is either null or has zero length after trimming

whitespace

public static boolean whitespace(String s)
Check whether the given String only contains whitespace characters. This method is null-safe. null will be interpreted as whitespace

Parameters:
s - the String
Returns:
true if the String consists only of whitespace characters or null.

replaceAll

public static String replaceAll(String source,
                                String pattern,
                                String replacement)
Replace a String inside of a String.

Parameters:
source - the String to replace stuff it
pattern - the String to replace
replacement - the replacement
Returns:
String the modified string

toHex

public static String toHex(byte[] data)
Convert byte array to hex string.

Parameters:
data - byte array
Returns:
hex string

toHex

public static String toHex(byte[] data,
                           int offset,
                           int len)
Convert byte array to hex string.

Parameters:
data - byte array
offset - from offset
len - length
Returns:
hex string

trimLeadingCharacter

public static String trimLeadingCharacter(String stringToTrim,
                                          char charToRemove)
Returns a copy of the string, with leading given parameter character omitted

Parameters:
stringToTrim - the string to trim
charToRemove - the character to remove
Returns:
A copy of this string with leading given character removed, or this string if it has no leading character.

trimTrailingCharacter

public static String trimTrailingCharacter(String stringToTrim,
                                           char charToRemove)
Returns a copy of the string, with trailing given parameter character omitted.

Parameters:
stringToTrim - the string to trim
charToRemove - the character to remove
Returns:
A copy of this string with trailing given character removed, or this string if it has no trailing given character.

trimCharacter

public static String trimCharacter(String stringToTrim,
                                   char charToRemove)
Returns a copy of the string, with leading and trailing given parameter character omitted.

Parameters:
stringToTrim - the string to trim
charToRemove - the character to remove
Returns:
A copy of this string with leading and trailing given character removed, or this string if it has no leading or trailing given character.

indexOf

public static int indexOf(byte[] buffer,
                          String matchString,
                          int fromIndex)
Finds the first occurrance of the given String in the given buffer.

Parameters:
buffer - The buffer to search for the given match String.
matchString - The character sequence to find in the given buffer
fromIndex - The index of the position to start the search with
Returns:
-1 if the String wasn't found; the index of its first character in the buffer otherwise

lastIndexOf

public static int lastIndexOf(byte[] buffer,
                              String matchString,
                              int toIndex)
Finds the last occurrance of the given String in the given buffer.

Parameters:
buffer - The buffer to search for the given match String.
matchString - The character sequence to find in the given buffer
toIndex - The index of the position to end the search with
Returns:
-1 if the String wasn't found; the index of its first character in the buffer otherwise

create

public static String create(InputStream is)
                     throws IOException
Create a string by fully reading the supplied input stream using the default platform encoding.

Parameters:
is -
Returns:
String
Throws:
IOException

create

public static String create(InputStream is,
                            String encoding)
                     throws IOException
Create a string by fully reading the supplied input stream using the specified encoding.

Parameters:
is -
encoding -
Returns:
String
Throws:
IOException

assertNotEmpty

public static void assertNotEmpty(String name,
                                  String value)
Checks whether the given value is an empty String

Parameters:
name -
value -

parsePrimitive

public static <T> T parsePrimitive(Class<T> targetType,
                                   String valueString)
Parses the valueString based on given targetType and returns a value object decoded from given valueString. The targetType defines the expected type of the returned value object. For primitive types use the primitive type class as targetType. For "int" as example use Integer.TYPE as targetType, the returned value will be wrapped as the corresponding object type such as Integer.

Parameters:
targetType - defines the expected return value type. Supported targetTypes are String, Byte, Short, Integer, Long, Double, Float, Boolean, Character, Enum, Color, Font.
valueString - the value as string
Returns:
a value object decoded version of valueString
Throws:
IllegalArgumentException - if the targetType is null or the valueString is badly formatted.

toFixedLength

public static String toFixedLength(String s,
                                   int length,
                                   char padding)
Format the string into a fixed-length representation by either trimming it to the required size (if it is longer) or padding it at the end with the given padding character.

Parameters:
s - the string
length - the desired length
padding - the padding character
Returns:
the result strimg which will be of the given length

asciiString

public static String asciiString(byte[] bytes)
Construct a new String from the given input bytes, using ASCII encoding.

Parameters:
bytes - the bytes to construct a String from
Returns:
the newly constructed String instance

utf8String

public static String utf8String(byte[] bytes)
Construct a new String from the given input bytes, using UTF-8 encoding.

Parameters:
bytes - the bytes to construct a String from
Returns:
the newly constructed String instance

asciiBytes

public static byte[] asciiBytes(String s)
Create a byte[] representation of the given string, using ASCII encoding.

Parameters:
s - the String to generate a byte[] representation of
Returns:
resulting byte[] representation of String's contents

utf8Bytes

public static byte[] utf8Bytes(String s)
Create a byte[] representation of the given string, using UTF-8 encoding.

Parameters:
s - the String to generate a byte[] representation of
Returns:
resulting byte[] representation of String's contents

utf16Bytes

public static byte[] utf16Bytes(String s)
Parameters:
s - the String to be encoded
Returns:
a byte[] representation of the given String, encoded as UTF-16, Big Endian with BOM


Copyright © 1995-2020 levigo holding gmbh. All Rights Reserved.