htsjdk.samtools.util
Class StringUtil

java.lang.Object
  extended by htsjdk.samtools.util.StringUtil

public class StringUtil
extends java.lang.Object

Grab-bag of stateless String-oriented utilities.


Field Summary
static java.lang.String EMPTY_STRING
          Returns Object.toString() of the provided value if it isn't null; "" otherwise.
 
Constructor Summary
StringUtil()
           
 
Method Summary
static java.lang.String asEmptyIfNull(java.lang.Object string)
           
static java.lang.String assertCharactersNotInString(java.lang.String illegalChars, char... chars)
          Checks that a String doesn't contain one or more characters of interest.
static java.lang.String bytesToHexString(byte[] data)
          Convert a byte array into a String hex representation.
static java.lang.String bytesToString(byte[] data)
           
static java.lang.String bytesToString(byte[] buffer, int offset, int length)
           
static char byteToChar(byte b)
          Convert ASCII byte to ASCII char.
static void charsToBytes(char[] chars, int charOffset, int length, byte[] bytes, int byteOffset)
          Convert chars to bytes merely by casting
static byte charToByte(char c)
          Convert ASCII char to byte.
static int fromHexDigit(char c)
           
static byte[] hexStringToBytes(java.lang.String s)
          Convert a String containing hex characters into an array of bytes with the binary representation of the hex string
static java.lang.String intValuesToString(int[] intVals)
           
static java.lang.String intValuesToString(short[] shortVals)
           
static boolean isBlank(java.lang.String str)
          Checks if a String is whitespace, empty ("") or null.
static
<T> java.lang.String
join(java.lang.String separator, java.util.Collection<T> objs)
           
static
<T> java.lang.String
join(java.lang.String separator, T... objs)
           
static int levenshteinDistance(java.lang.String string1, java.lang.String string2, int swap, int substitution, int insertion, int deletion)
           
static java.lang.String readNullTerminatedString(BinaryCodec binaryCodec)
           
static java.lang.String repeatCharNTimes(char c, int repeatNumber)
           
static java.lang.String reverseString(java.lang.String s)
          Reverse the given string.
static int split(java.lang.String aString, java.lang.String[] tokens, char delim)
          Split the string into tokens separated by the given delimiter.
static int splitConcatenateExcessTokens(java.lang.String aString, java.lang.String[] tokens, char delim)
          Split the string into tokens separated by the given delimiter.
static byte[] stringToBytes(java.lang.String s)
           
static byte[] stringToBytes(java.lang.String s, int offset, int length)
           
static char toHexDigit(int value)
           
static byte toLowerCase(byte b)
           
static byte toUpperCase(byte b)
           
static void toUpperCase(byte[] bytes)
          Converts in place all lower case letters to upper case in the byte array provided.
static java.lang.String wordWrap(java.lang.String s, int maxLineLength)
          Return input string with newlines inserted to ensure that all lines have length <= maxLineLength.
static java.lang.String wordWrapSingleLine(java.lang.String s, int maxLineLength)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_STRING

public static final java.lang.String EMPTY_STRING
Returns Object.toString() of the provided value if it isn't null; "" otherwise.

See Also:
Constant Field Values
Constructor Detail

StringUtil

public StringUtil()
Method Detail

join

public static <T> java.lang.String join(java.lang.String separator,
                                        java.util.Collection<T> objs)
Parameters:
separator - String to interject between each string in strings arg
objs - List of objs to be joined
Returns:
String that concatenates the result of each item's to String method for all items in objs, with separator between each of them.

join

public static <T> java.lang.String join(java.lang.String separator,
                                        T... objs)

split

public static int split(java.lang.String aString,
                        java.lang.String[] tokens,
                        char delim)
Split the string into tokens separated by the given delimiter. Profiling has revealed that the standard string.split() method typically takes > 1/2 the total time when used for parsing ascii files. Note that if tokens arg is not large enough to all the tokens in the string, excess tokens are discarded.

Parameters:
aString - the string to split
tokens - an array to hold the parsed tokens
delim - character that delimits tokens
Returns:
the number of tokens parsed

splitConcatenateExcessTokens

public static int splitConcatenateExcessTokens(java.lang.String aString,
                                               java.lang.String[] tokens,
                                               char delim)
Split the string into tokens separated by the given delimiter. Profiling has revealed that the standard string.split() method typically takes > 1/2 the total time when used for parsing ascii files. Note that the string is split into no more elements than tokens arg will hold, so the final tokenized element may contain delimiter chars.

Parameters:
aString - the string to split
tokens - an array to hold the parsed tokens
delim - character that delimits tokens
Returns:
the number of tokens parsed

toLowerCase

public static byte toLowerCase(byte b)
Parameters:
b - ASCII character
Returns:
lowercase version of arg if it was uppercase, otherwise returns arg

toUpperCase

public static byte toUpperCase(byte b)
Parameters:
b - ASCII character
Returns:
uppercase version of arg if it was lowercase, otherwise returns arg

toUpperCase

public static void toUpperCase(byte[] bytes)
Converts in place all lower case letters to upper case in the byte array provided.


assertCharactersNotInString

public static java.lang.String assertCharactersNotInString(java.lang.String illegalChars,
                                                           char... chars)
Checks that a String doesn't contain one or more characters of interest.

Parameters:
illegalChars - the String to check
chars - the characters to check for
Returns:
String the input String for convenience
Throws:
java.lang.IllegalArgumentException - if the String contains one or more of the characters

wordWrap

public static java.lang.String wordWrap(java.lang.String s,
                                        int maxLineLength)
Return input string with newlines inserted to ensure that all lines have length <= maxLineLength. if a word is too long, it is simply broken at maxLineLength. Does not handle tabs intelligently (due to implementer laziness).


wordWrapSingleLine

public static java.lang.String wordWrapSingleLine(java.lang.String s,
                                                  int maxLineLength)

intValuesToString

public static java.lang.String intValuesToString(int[] intVals)

intValuesToString

public static java.lang.String intValuesToString(short[] shortVals)

bytesToString

public static java.lang.String bytesToString(byte[] data)

bytesToString

public static java.lang.String bytesToString(byte[] buffer,
                                             int offset,
                                             int length)

stringToBytes

public static byte[] stringToBytes(java.lang.String s)

stringToBytes

public static byte[] stringToBytes(java.lang.String s,
                                   int offset,
                                   int length)

readNullTerminatedString

public static java.lang.String readNullTerminatedString(BinaryCodec binaryCodec)

charsToBytes

public static void charsToBytes(char[] chars,
                                int charOffset,
                                int length,
                                byte[] bytes,
                                int byteOffset)
Convert chars to bytes merely by casting

Parameters:
chars - input chars
charOffset - where to start converting from chars array
length - how many chars to convert
bytes - where to put the converted output
byteOffset - where to start writing the converted output.

charToByte

public static byte charToByte(char c)
Convert ASCII char to byte.


byteToChar

public static char byteToChar(byte b)
Convert ASCII byte to ASCII char.


bytesToHexString

public static java.lang.String bytesToHexString(byte[] data)
Convert a byte array into a String hex representation.

Parameters:
data - Input to be converted.
Returns:
String twice as long as data.length with hex representation of data.

hexStringToBytes

public static byte[] hexStringToBytes(java.lang.String s)
                               throws java.lang.NumberFormatException
Convert a String containing hex characters into an array of bytes with the binary representation of the hex string

Parameters:
s - Hex string. Length must be even because each pair of hex chars is converted into a byte.
Returns:
byte array with binary representation of hex string.
Throws:
java.lang.NumberFormatException

toHexDigit

public static char toHexDigit(int value)

fromHexDigit

public static int fromHexDigit(char c)
                        throws java.lang.NumberFormatException
Throws:
java.lang.NumberFormatException

reverseString

public static java.lang.String reverseString(java.lang.String s)
Reverse the given string. Does not check for null.

Parameters:
s - String to be reversed.
Returns:
New string that is the reverse of the input string.

isBlank

public static boolean isBlank(java.lang.String str)

Checks if a String is whitespace, empty ("") or null.

 StringUtils.isBlank(null)      = true
 StringUtils.isBlank("")        = true
 StringUtils.isBlank(" ")       = true
 StringUtils.isBlank("sam")     = false
 StringUtils.isBlank("  sam  ") = false
 

Parameters:
str - the String to check, may be null
Returns:
true if the String is null, empty or whitespace

repeatCharNTimes

public static java.lang.String repeatCharNTimes(char c,
                                                int repeatNumber)

asEmptyIfNull

public static java.lang.String asEmptyIfNull(java.lang.Object string)

levenshteinDistance

public static int levenshteinDistance(java.lang.String string1,
                                      java.lang.String string2,
                                      int swap,
                                      int substitution,
                                      int insertion,
                                      int deletion)