Class StringUtils
- java.lang.Object
-
- com.facilita.util.StringUtils
-
public class StringUtils extends java.lang.ObjectProvides simple static methods for string manipulation
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringconcatString(java.lang.String[] p, java.lang.String token)Concatenates aStringarray into a singleString, using thetokenparameter as a separator.static java.lang.StringexpandEnvironmentVariables(java.lang.String arg)Expands any environment variables in the specified text.static java.lang.StringfindInbetween(java.lang.String text, java.lang.String startPattern, java.lang.String endPattern, boolean ignoreCase)Searches for twoStringobjects intextand returns the text between them, or an empty string if the patterns could not be matched.static byte[]hexToBinary(java.lang.String hex)Returns a byte array containing the values of a sequence of hexadecimal numbers in thehex Stringparameter.static java.lang.StringhexToString(byte[] hex)Returns a hexadecimal string representation of the byte values in the hex parameter.static java.lang.StringsimplifyWhitespace(java.lang.String text)Replaces one or more consecutive white space characters by a single space character, and trims white space from each end of aString.static java.lang.String[]splitString(java.lang.String p, java.lang.String token)Splits aStringbased upon any character in the specifiedtoken.static java.util.ArrayList<java.lang.String>splitStringExcel(java.lang.String line, java.lang.String delim)Split aStringusing Microsoft Excel-style quoting.
-
-
-
Method Detail
-
splitString
public static java.lang.String[] splitString(java.lang.String p, java.lang.String token)Splits aStringbased upon any character in the specifiedtoken.The string is split on any individual character in the
tokenparameter, not the string as a whole. e.g.String[] tokens = StringUtils.splitString("Split me up! Word1 Word2 Word3.", " o"); // tokens now contains { "Split", "me", "up!", "W", "rd1", "W", "rd2", "W", "rd3." } for (String token: tokens) { writeMessage("Token: " + token); }- Parameters:
p- theStringto splittoken- the characters to use as delimiters- Returns:
- an array of
Stringobjects
-
splitStringExcel
public static java.util.ArrayList<java.lang.String> splitStringExcel(java.lang.String line, java.lang.String delim)Split aStringusing Microsoft Excel-style quoting.- Parameters:
line- theStringto splitdelim- the token to use as delimiters- Returns:
- a list of
Stringobjects
-
concatString
public static java.lang.String concatString(java.lang.String[] p, java.lang.String token)Concatenates aStringarray into a singleString, using thetokenparameter as a separator.e.g.
String value = StringUtils.concatString(new String[] { "item1", "item2", "item3" }, ", "); writeMessage(value);- Parameters:
p- the array ofStringobjects to concatenatetoken- the delimiter to place between theStringobjects- Returns:
- the concatenated
String
-
expandEnvironmentVariables
public static java.lang.String expandEnvironmentVariables(java.lang.String arg)
Expands any environment variables in the specified text.Any environment variables in the form
%VARIABLE%will be replaced with the value of the environment variable on the injector machine at runtime.- Parameters:
arg- the text to parse- Returns:
- the text, with any environment variables expanded
-
findInbetween
public static java.lang.String findInbetween(java.lang.String text, java.lang.String startPattern, java.lang.String endPattern, boolean ignoreCase)Searches for twoStringobjects intextand returns the text between them, or an empty string if the patterns could not be matched.e.g.
// If the web page contained the string ...name="John Smith">, this call will return // the text "John Smith" (excluding the quotes) String page = response.getContent(); // get the contents of the response String myValue = StringUtils.findInbetween(page, "name=\"", "\">", true); // find the text between value=" and ">- Parameters:
text- theStringto search instartPattern- aStringwhich occurs immediately before the text to be extractedendPattern- aStringwhich occurs immediately after the text to be extractedignoreCase-trueif the search should be case-independent- Returns:
- the
Stringfound between the start and end patterns, or an emptyStringif there is no match
-
simplifyWhitespace
public static java.lang.String simplifyWhitespace(java.lang.String text)
Replaces one or more consecutive white space characters by a single space character, and trims white space from each end of aString.White space comprises of the characters
' \t\n\r'- space, tab, line feed and newline.e.g.
String s = " This is a\t\rstring\n\nwith white space "; s = StringUtils.simplifyWhitespace(s); // s now contains "This is a string with white space"- Parameters:
text- theStringto be modified- Returns:
- the modified
String
-
hexToBinary
public static byte[] hexToBinary(java.lang.String hex)
Returns a byte array containing the values of a sequence of hexadecimal numbers in thehex Stringparameter.This method is used to convert a
Stringcontaining the hexadecimal representation of a sequence of bytes into a byte array containing the binary values.The input string must contain pairs of hexadecimal characters; white space or any non hexadecimal character between each pair is ignored.
If the second character of an expected pair of hexadecimal characters is not a valid hex character, the conversion is terminated.
Valid input format examples:
"8014833031F300AB15974D4252204157""80 14 83 f3 31 F3 00 ab 15 97 4d 42 ef 20 41 57""x80 x14 xAA x30""80 1483xAAx31 \\x33 0080bcef4d4252204157"
e.g.
String EBCDIC = "C2C9C8C4F0F1F8F04040F0F240404040404040404040E2D"; byte[] hex = StringUtils.hexToBinary("EBCDIC"); writeMessage(StringUtils.hexToString(hex)); // display contents- Parameters:
hex- the inputString, composed of pairs of hexadecimal characters where each pair represents a single byte- Returns:
- the binary values of the hexadecimal bytes in the input string
- See Also:
hexToString(byte[])
-
hexToString
public static java.lang.String hexToString(byte[] hex)
Returns a hexadecimal string representation of the byte values in the hex parameter.The returned string is in the format typically used to display binary data such as in an ethernet trace or the log file output from a TCP Virtual User.
Typical output format example:
"80 14 83 f3 31 F3 00 ab 15 97 4d 42 ef 20 41 57"
- Parameters:
hex- the input byte array- Returns:
- a hexadecimal string representation of the byte values
- See Also:
hexToBinary(String)
-
-