org.foray.common
Class StringUtil

java.lang.Object
  extended by org.foray.common.StringUtil

public final class StringUtil
extends Object

Utility class containing methods that are useful for manipulating or managing strings.


Field Summary
static String EMPTY_STRING
          An empty string suitable for String comparisons or initializations.
static String LINE_SEPARATOR
          A static capture of the system line separator.
static char NULL_TERMINATOR
          The null terminator character, used to mark the end of Strings in char arrays in some applications, U+0000.
static String SINGLE_SPACE
          A single space.
 
Method Summary
static byte asciiHexBytesToHex(byte byte1, byte byte2)
          Converts two bytes, each of which must be valid ASCII hex (0-9, A-F, a-f) into the byte that those ASCII characters represent.
static byte asciiHexByteToHex(byte inputByte)
          Converts one ASCII Hex byte (0-9, A-F, a-f) to the hexadecimal byte that it represents.
static String charToHexString(char c, boolean upperCase, int minimumSize)
          Returns a String representing a character's hex value.
static String charToOctalString(char c, int minimumSize)
          Returns a String representing a character's octal value.
static int firstDifferent(String string1, String string2)
          Compares two Strings for equality, returning the index to the first different character.
static int indexOfSortedCharArray(char[] charArray, char inputChar, int startIndex, int endIndex)
          Efficiently finds the index within charArray, if any, that matches inputChar.
static boolean isASCIIHex(int input)
          Reports whether an input character is a valid hexadecimal character.
static void lineBreakStringBuffer(StringBuilder buffer, String eol, int length)
          Adds linefeeds to the contents of a StringBuffer.
static int nullTerminatedLength(char[] charArray, int start)
          Computes the length of all or part of a null-terminated char array.
static int nullTerminatedLength(CharSequence charSequence, int start)
          Computes the length of all or part of a null-terminated String or other CharSequence.
static int replace(StringBuilder buffer, String oldString, String newString)
          Replaces all occurrences of a given substring with a different substring.
static void toChars(CharSequence source, int srcBegin, int srcEnd, char[] dst, int dstBegin)
          Copies characters from a CharSequence string into a destination char[].
static int[] toCodePoints(char[] input)
          Converts a char[] to an array of Unicode code points.
static int[] toCodePoints(CharSequence input)
          Converts a CharSequence to an array of Unicode code points.
 
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 String EMPTY_STRING
An empty string suitable for String comparisons or initializations.

See Also:
Constant Field Values

SINGLE_SPACE

public static final String SINGLE_SPACE
A single space.

See Also:
Constant Field Values

LINE_SEPARATOR

public static final String LINE_SEPARATOR
A static capture of the system line separator.


NULL_TERMINATOR

public static final char NULL_TERMINATOR
The null terminator character, used to mark the end of Strings in char arrays in some applications, U+0000.

See Also:
Constant Field Values
Method Detail

isASCIIHex

public static boolean isASCIIHex(int input)
Reports whether an input character is a valid hexadecimal character. Valid hex characters are 0-9, A-F, and a-f.

Parameters:
input - The character to be tested.
Returns:
True iff the character is a valid hexadecimal character.

asciiHexBytesToHex

public static byte asciiHexBytesToHex(byte byte1,
                                      byte byte2)
Converts two bytes, each of which must be valid ASCII hex (0-9, A-F, a-f) into the byte that those ASCII characters represent. For example, the bytes "5" (0x35) and "9" (0x39) should be converted into a "Y" (0x59).

Parameters:
byte1 - The high-order byte to be converted.
byte2 - The low-order byte to be converted.
Returns:
The byte containing the hex value of the 2 input bytes, or -1 if the input is valid.

asciiHexByteToHex

public static byte asciiHexByteToHex(byte inputByte)
Converts one ASCII Hex byte (0-9, A-F, a-f) to the hexadecimal byte that it represents.

Parameters:
inputByte - The ASCII Hex byte to convert.
Returns:
The hex equivalent of inputByte.

indexOfSortedCharArray

public static int indexOfSortedCharArray(char[] charArray,
                                         char inputChar,
                                         int startIndex,
                                         int endIndex)
Efficiently finds the index within charArray, if any, that matches inputChar. Note: charArray must be already sorted. No checking of this condition is performed.

Parameters:
charArray - The array of char items which should be searched to find a match for inputChar.
inputChar - The char for which a match is sought.
startIndex - The first index in the array to be considered.
endIndex - The last index in the array to be considered.
Returns:
The index (0 through stringArray.length - 1) of the matching element, or -1 if no match is found.

charToHexString

public static String charToHexString(char c,
                                     boolean upperCase,
                                     int minimumSize)
Returns a String representing a character's hex value. For example, using the character "Y" (U+0059) as input should return "59", and using "m" (U+006D) as input should return "6D".

Parameters:
c - The character to be converted.
upperCase - Set to true if the a-f hex characters should be converted to uppercase.
minimumSize - The minimum size of the return String. The return value will be padded with sufficient "0" characters at the beginning to ensure this size.
Returns:
The String representing c's hex value.

charToOctalString

public static String charToOctalString(char c,
                                       int minimumSize)
Returns a String representing a character's octal value. For example, using the character "Y" (U+0059) as input should return "131", and using "m" (U+006D) as input should return "155".

Parameters:
c - The character to be converted.
minimumSize - The minimum size of the return String. The return value will be padded with sufficient "0" characters at the beginning to ensure this size.
Returns:
The String representing c's octal value.

lineBreakStringBuffer

public static void lineBreakStringBuffer(StringBuilder buffer,
                                         String eol,
                                         int length)
Adds linefeeds to the contents of a StringBuffer.

Parameters:
buffer - The StringBuffer to which line-breaks should be added.
eol - The String that should be used for line-breaking. Note that indenting can be added to the start of a line by simply adding some spaces to the end of eol.
length - The maximum length that a line should contain.

toCodePoints

public static int[] toCodePoints(CharSequence input)
Converts a CharSequence to an array of Unicode code points.

Parameters:
input - The CharSequence to be converted.
Returns:
An array of Unicode code points.

toCodePoints

public static int[] toCodePoints(char[] input)
Converts a char[] to an array of Unicode code points.

Parameters:
input - The char[] to be converted.
Returns:
An array of Unicode code points.

toChars

public static void toChars(CharSequence source,
                           int srcBegin,
                           int srcEnd,
                           char[] dst,
                           int dstBegin)
Copies characters from a CharSequence string into a destination char[]. This mimics the logic from String.getChars(int, int, char[], int).

The first character to be copied is at index srcBegin; the last character to be copied is at index srcEnd-1 (thus the total number of characters to be copied is srcEnd-srcBegin). The characters are copied into the subarray of dst starting at index dstBegin and ending at index:

     dstbegin + (srcEnd-srcBegin) - 1
 

Parameters:
source - The CharSequence from which the chars will be copied.
srcBegin - Index of the first character in the CharSequence to copy.
srcEnd - Index after the last character in the CharSequence to copy.
dst - The destination array.
dstBegin - The start offset in the destination array.
See Also:
String.getChars(int, int, char[], int)

firstDifferent

public static int firstDifferent(String string1,
                                 String string2)
Compares two Strings for equality, returning the index to the first different character.

Parameters:
string1 - The first String being compared.
string2 - The second String being compared.
Returns:
The index to the first character in the Strings that is different. If the Strings are identical, returns -1.

replace

public static int replace(StringBuilder buffer,
                          String oldString,
                          String newString)
Replaces all occurrences of a given substring with a different substring.

Parameters:
buffer - The SringBuffer in which the replacement should occur.
oldString - The substring to be replaced by newString.
newString - The substring that should replace oldString.
Returns:
The number of replacements that were made.

nullTerminatedLength

public static int nullTerminatedLength(char[] charArray,
                                       int start)
Computes the length of all or part of a null-terminated char array. The null character is 0x00.

Parameters:
charArray - The array which contains the null-terminated string.
start - The index to the first char in charArray which should be counted.
Returns:
The number of chars before the first null character. If no null character is found, the returned value is the count of chars to the end of the array.

nullTerminatedLength

public static int nullTerminatedLength(CharSequence charSequence,
                                       int start)
Computes the length of all or part of a null-terminated String or other CharSequence. The null character is 0x00.

Parameters:
charSequence - The CharSequence which contains the null-terminated string.
start - The index to the first char in charSequence which should be counted.
Returns:
The number of chars before the first null character. If no null character is found, the returned value is the count of chars to the end of the array.


Copyright © 2017. All rights reserved.