Package ucar.unidata.util
Class StringUtil2
- java.lang.Object
-
- ucar.unidata.util.StringUtil2
-
public class StringUtil2 extends Object
Static String and StringBuilder utilities- Since:
- 7/29/11
-
-
Constructor Summary
Constructors Constructor Description StringUtil2()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static String
allow(String x, String allowChars, char replaceChar)
Replace any char not alphanumeric or in allowChars by replaceChar.static String
breakTextAtWords(String text, String insert, int lineSize)
Break the given text into lines, respecting word boundaries (blank space).static String
cleanup(byte[] h)
Delete any non-printable charactersstatic String
cleanup(String s)
static String
collapseWhitespace(String s)
Collapse continuous whitespace into one single " ".static String
escape(String x, String okChars)
Escape any char not alphanumeric or in okChars.static String
filter(String x, String okChars)
Remove any char not alphanumeric or in okChars.static String
filter7bits(String s)
Remove all but printable asciistatic List<String>
getTokens(String fullString, String sep)
static String
makeValidCdmObjectName(String name)
static int
match(String s1, String s2)
Count number of chars that match in two strings, starting from front.static String
padLeft(String s, int desiredLength)
Pad the given string with spaces on the left up to the given length.static String
padLeft(String s, int desiredLength, String padString)
Pad the given string with padString on the left up to the given length.static String
padRight(String s, int desiredLength)
Pad the given string with spaces on the right up to the given length.static String
padRight(String s, int desiredLength, String padString)
Pad the given string with padString on the right up to the given length.static String
padZero(int value, int numDigits)
Left pad the given value with zeros up to the number of digitsstatic String
quoteHtmlContent(String x)
Deprecated.legacy only, use HtmlEscapers.htmlEscaper()static String
remove(String s, int c)
Remove all occurrences of the character c in the string s.static String
remove(String s, String sub)
Remove all occurrences of the substring sub in the string s.static void
removeAll(StringBuilder sb, String out)
Remove any of the characters in out from sbstatic String
removeFromEnd(String s, int c)
Remove all occurrences of the character c at the end of s.static String
removeFromEnd(String s, String suffix)
static String
removeWhitespace(String inputString)
Remove any whitespace (ie., Character.isWhitespace) from the input string.static void
replace(StringBuilder sb, char out, String in)
Replace any char "out" in sb with String "in".static void
replace(StringBuilder sb, String out, String in)
Replace any of the characters from out with corresponding character from instatic String
replace(String x, char[] replaceChar, String[] replaceWith)
Replace all occurrences of any char in replaceChar with corresponding String in replaceWithstatic String
replace(String s, char out, String in)
Replace any char "out" in s with "in".static String
replace(String string, String pattern, String value)
Replaces all occurrences of "pattern" in "string" with "value"static Iterable<String>
split(String source)
static String[]
splitString(String source)
Deprecated.use StringUtil2.split()static void
substitute(StringBuilder sbuff, String match, String subst)
Find all occurences of the "match" in original, and substitute the "subst" string, directly into the original.static String
substitute(String original, String[] match, String[] subst)
Find all occurences of match strings in original, and substitute the corresponding subst string.static String
substitute(String original, String match, String subst)
Deprecated.use original.replace(match, subst)static String
trim(String s, int bad)
Remove bad char from beginning or end of stringstatic String
unescape(String x)
This finds any '%xx' and converts to the equivalent char.static void
unreplace(StringBuilder sb, String out, char in)
Replace any String "out" in sb with char "in".static String
unreplace(String x, String[] orgReplace, char[] orgChar)
Replace all occurrences of orgReplace with orgChar; inverse of replace().
-
-
-
Method Detail
-
allow
public static String allow(String x, String allowChars, char replaceChar)
Replace any char not alphanumeric or in allowChars by replaceChar.- Parameters:
x
- operate on this stringallowChars
- these are ok.replaceChar
- thar char to replace- Returns:
- resulting string.
-
breakTextAtWords
public static String breakTextAtWords(String text, String insert, int lineSize)
Break the given text into lines, respecting word boundaries (blank space).- Parameters:
text
- The text to convertinsert
- break to insertlineSize
- line size to insert at- Returns:
- The text with added br tags.
-
cleanup
public static String cleanup(byte[] h)
Delete any non-printable characters- Parameters:
h
- byte array- Returns:
- cleaned up string
-
filter
public static String filter(String x, String okChars)
Remove any char not alphanumeric or in okChars.- Parameters:
x
- filter this stringokChars
- these are ok.- Returns:
- filtered string.
-
filter7bits
public static String filter7bits(String s)
Remove all but printable ascii- Parameters:
s
- filter this string- Returns:
- filtered string.
-
match
public static int match(String s1, String s2)
Count number of chars that match in two strings, starting from front.- Parameters:
s1
- compare this strings2
- compare this string- Returns:
- number of matching chars, starting from first char
-
padZero
public static String padZero(int value, int numDigits)
Left pad the given value with zeros up to the number of digits- Parameters:
value
- The value.numDigits
- number of digits- Returns:
- The String represenation of the value, padded with leading "0"-s if value < 10E(numDigits-1)
-
padLeft
public static String padLeft(String s, int desiredLength)
Pad the given string with spaces on the left up to the given length.- Parameters:
s
- String to paddesiredLength
- ending length- Returns:
- padded String
-
padLeft
public static String padLeft(String s, int desiredLength, String padString)
Pad the given string with padString on the left up to the given length.- Parameters:
s
- String to paddesiredLength
- ending lengthpadString
- String to pad with (e.g, " ")- Returns:
- padded String
-
padRight
public static String padRight(String s, int desiredLength)
Pad the given string with spaces on the right up to the given length.- Parameters:
s
- String to paddesiredLength
- ending length- Returns:
- padded String
-
padRight
public static String padRight(String s, int desiredLength, String padString)
Pad the given string with padString on the right up to the given length.- Parameters:
s
- String to paddesiredLength
- ending lengthpadString
- String to pad with (e.g, " ")- Returns:
- padded String
-
remove
public static String remove(String s, String sub)
Remove all occurrences of the substring sub in the string s.- Parameters:
s
- operate on this stringsub
- remove all occurrences of this substring.- Returns:
- result with substrings removed
-
remove
public static String remove(String s, int c)
Remove all occurrences of the character c in the string s.- Parameters:
s
- operate on this stringc
- remove all occurrences of this character.- Returns:
- result with any character c removed
-
removeFromEnd
public static String removeFromEnd(String s, int c)
Remove all occurrences of the character c at the end of s.- Parameters:
s
- operate on this stringc
- remove all occurrences of this character that are at the end of the string.- Returns:
- result with any character c removed
-
removeWhitespace
public static String removeWhitespace(String inputString)
Remove any whitespace (ie., Character.isWhitespace) from the input string.- Parameters:
inputString
- The string to remove the whitespace.- Returns:
- The whitespaceless result.
-
collapseWhitespace
public static String collapseWhitespace(String s)
Collapse continuous whitespace into one single " ".- Parameters:
s
- operate on this string- Returns:
- result with collapsed whitespace
-
replace
public static String replace(String s, char out, String in)
Replace any char "out" in s with "in".- Parameters:
s
- string to replaceout
- replace this characterin
- with this string- Returns:
- modified string if needed
-
replace
public static String replace(String x, char[] replaceChar, String[] replaceWith)
Replace all occurrences of any char in replaceChar with corresponding String in replaceWith- Parameters:
x
- operate on this stringreplaceChar
- get rid of thesereplaceWith
- replace with these- Returns:
- resulting string
-
replace
public static String replace(String string, String pattern, String value)
Replaces all occurrences of "pattern" in "string" with "value"- Parameters:
string
- string to mungepattern
- pattern to replacevalue
- replacement value- Returns:
- munged string
-
unreplace
public static String unreplace(String x, String[] orgReplace, char[] orgChar)
Replace all occurrences of orgReplace with orgChar; inverse of replace().- Parameters:
x
- operate on this stringorgReplace
- get rid of theseorgChar
- replace with these- Returns:
- resulting string
-
substitute
@Deprecated public static String substitute(String original, String match, String subst)
Deprecated.use original.replace(match, subst)Find all occurrences of the "match" in original, and substitute the "subst" string.- Parameters:
original
- starting stringmatch
- string to matchsubst
- string to substitute- Returns:
- a new string with substitutions
-
escape
public static String escape(String x, String okChars)
Escape any char not alphanumeric or in okChars. Escape by replacing char with %xx (hex).- Parameters:
x
- escape this stringokChars
- these are ok.- Returns:
- equivilent escaped string.
-
unescape
public static String unescape(String x)
This finds any '%xx' and converts to the equivalent char. Inverse of escape().- Parameters:
x
- operate on this String- Returns:
- original String.
-
splitString
@Deprecated public static String[] splitString(String source)
Deprecated.use StringUtil2.split()Split a string on one or more whitespace. Cover for String.split, because who can remember regexp?- Parameters:
source
- split this string- Returns:
- tokens that were separated by whitespace
-
substitute
public static String substitute(String original, String[] match, String[] subst)
Find all occurences of match strings in original, and substitute the corresponding subst string.- Parameters:
original
- starting stringmatch
- array of strings to matchsubst
- array of strings to substitute- Returns:
- a new string with substitutions
-
removeAll
public static void removeAll(StringBuilder sb, String out)
Remove any of the characters in out from sb- Parameters:
sb
- the StringBuilderout
- get rid of any of these characters
-
replace
public static void replace(StringBuilder sb, char out, String in)
Replace any char "out" in sb with String "in".- Parameters:
sb
- StringBuilder to replaceout
- repalce this characterin
- with this string
-
unreplace
public static void unreplace(StringBuilder sb, String out, char in)
Replace any String "out" in sb with char "in".- Parameters:
sb
- StringBuilder to replaceout
- repalce this Stringin
- with this char
-
replace
public static void replace(StringBuilder sb, String out, String in)
Replace any of the characters from out with corresponding character from in- Parameters:
sb
- the StringBuilderout
- get rid of any of these charactersin
- replacing with the character at same index
-
substitute
public static void substitute(StringBuilder sbuff, String match, String subst)
Find all occurences of the "match" in original, and substitute the "subst" string, directly into the original.- Parameters:
sbuff
- starting string buffermatch
- string to matchsubst
- string to substitute
-
trim
public static String trim(String s, int bad)
Remove bad char from beginning or end of string- Parameters:
s
- operate on this- Returns:
- trimmed string
-
-