Package org.kissweb
Class StringUtils
java.lang.Object
org.kissweb.StringUtils
This class contains many methods used to manipulate
String
s.-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
Strip the spaces on both ends of a string.static String
ConvertCharacter
toString
correctly handling nulls.static String
An APL-like DROP function.static String
findClosestMatch
(Vector<String> strings, String input) Finds the string from the given vector of strings that most closely matches the input string.static String
htmlToText
(String html) Converts HTML text to plain text.static boolean
String null or empty test.static String
Join rows or a String array into a String separated by a delimiter.static String
Strip the spaces at the beginning of a string.static char[]
movmem
(char[] f, int fi, char[] t, int ti, int n) Moves characters from one array to another.static String
rightStrip
(String s) Strip the spaces of the end of a string.static String[]
Splits a string into an array of substrings based on a specified delimiter character.static String
C-like sprintf method.static Character
ConvertString
toCharacter
correctly handling nulls and zero length strings.static int
strLength
(char[] v) static String
This method provides the ability to get a substring of a string by providing a starting location and a length.static char[]
take
(char[] s, int n) static String
An APL-like TAKE function.static String
Converts a string to ASCII by replacing non-ASCII characters with their ASCII approximations and removing other non-ASCII characters.
-
Constructor Details
-
StringUtils
public StringUtils()
-
-
Method Details
-
substring
This method provides the ability to get a substring of a string by providing a starting location and a length.- Parameters:
str
- the String to be substring'edfrom
- index of the starting position (starting at 0)n
- number of characters to take- Returns:
- the sub-string
-
take
An APL-like TAKE function. Assures that the string isn
characters long.
If the string is shorter thann
it is padded by spaces.
If the string is longer thann
it is cut short.
Ifn
is negative, the same thing occurs but it starts at the back of the string. Thus padding or cutting of the string occur at the start of the string.- Parameters:
s
-n
-- Returns:
- See Also:
-
take
public static char[] take(char[] s, int n) -
strLength
public static int strLength(char[] v) -
movmem
public static char[] movmem(char[] f, int fi, char[] t, int ti, int n) Moves characters from one array to another. The two arrays may be the same array. Correctly handles overlapping regions.- Parameters:
f
- the from or source arrayfi
- the index into f to start att
- the to or destination arrayti
- the starting index into tn
- the number of characters to move- Returns:
- the destination array
-
drop
An APL-like DROP function. Dropsn
characters from the string.
Ifn
is positive,n
characters get dropped from the beginning of the string.
Ifn
is negative,n
characters get dropped from the end of the string.
Ifn
is greater than the length of the string, "" is returned.- Parameters:
s
-n
-- Returns:
- See Also:
-
rightStrip
Strip the spaces of the end of a string.- Parameters:
s
-- Returns:
-
leftStrip
Strip the spaces at the beginning of a string.- Parameters:
s
-- Returns:
-
centerStrip
Strip the spaces on both ends of a string.- Parameters:
s
-- Returns:
-
sprintf
C-like sprintf method. Format a string according to the format inserting the arguments as placed in the format string.- Parameters:
fmt
- format stringarg1
- consecutive values used by the format string- Returns:
- See Also:
-
join
Join rows or a String array into a String separated by a delimiter.- Parameters:
strings
-separator
-- Returns:
-
isEmpty
String null or empty test. Returns true if string is null or empty.- Parameters:
str
-- Returns:
-
stringToCharacter
ConvertString
toCharacter
correctly handling nulls and zero length strings.- Parameters:
s
-- Returns:
-
characterToString
ConvertCharacter
toString
correctly handling nulls.- Parameters:
c
-- Returns:
-
split
Splits a string into an array of substrings based on a specified delimiter character. Unlike the Java version, this one correctly handles empty strings.- Parameters:
s
- the string to be splitr
- the delimiter regex- Returns:
- an array of substrings
-
htmlToText
Converts HTML text to plain text. Also corrects some Unicode characters.- Parameters:
html
- the HTML text to be converted- Returns:
- the converted plain text
-
findClosestMatch
Finds the string from the given vector of strings that most closely matches the input string.
This method normalizes the input and the vector strings by removing spaces and converting to lowercase before comparing them using the Jaro-Winkler distance algorithm.- Parameters:
strings
- a vector of strings to compare against the input stringinput
- the input string that may contain typos, case, or space differences- Returns:
- the string from the vector that most closely matches the input string
-
toAscii
Converts a string to ASCII by replacing non-ASCII characters with their ASCII approximations and removing other non-ASCII characters.- Parameters:
input
- the original string- Returns:
- the ASCII string
-