Package com.gentlyweb.utils
Class StringUtils
- java.lang.Object
-
- com.gentlyweb.utils.StringUtils
-
public class StringUtils extends Object
-
-
Constructor Summary
Constructors Constructor Description StringUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
areAllCharsInRange(String value, char start, char end)
Indicate whether the String value has ALL of it's characters in the specified range of characters.static String
convertStringToCharEntities(String str)
Convert a string to a set of character entities.static String
getTextAsFormattedLines(String text, int lineLength)
Given a chunk of text format it so that you return a new String with the line length equal to that passed in.static boolean
hasCharInRange(String value, char start, char end)
Indicate whether the String value has at least one character in the range specified.static boolean
hasValue(String v)
Determine whether the specified string contains a value, where having a value means:(string != null) && (!string.equals (""))
static String
removeChar(String str, char c)
Given a particular String, remove any instances of the given character from it and return a new String.static String
replaceAllStrings(String text, Map map)
Given a Map of String/String (or Object/Object, they will be converted to Strings before replacement) replace all instances of each one in the specified text.static String
replaceAllStrings(String text, Map map, boolean caseSensitive)
Given a Map of String/String (or Object/Object, they will be converted to Strings before replacement) replace all instances of each one in the specified text.static String
replaceString(String text, String str, String replace)
Replace all instances of the specified string in the text given.static String
replaceString(String text, String str, String replace, boolean caseSensitive)
Replace all instances of the specified string in the text given.static List
tokenizeString(String str, String token)
A method to chop up a String on another multi-character String and then return the results in a List.
-
-
-
Method Detail
-
convertStringToCharEntities
public static String convertStringToCharEntities(String str)
Convert a string to a set of character entities.- Parameters:
str
- The string to convert.- Returns:
- The converted string.
-
getTextAsFormattedLines
public static String getTextAsFormattedLines(String text, int lineLength)
Given a chunk of text format it so that you return a new String with the line length equal to that passed in.- Parameters:
text
- The text to format.lineLength
- The line length to format the text to.- Returns:
- The formatted text.
-
hasCharInRange
public static boolean hasCharInRange(String value, char start, char end)
Indicate whether the String value has at least one character in the range specified.- Parameters:
value
- The value to check.start
- The starting character of the range.end
- The end character of the range.- Returns:
- Return
true
if it has at least one character in the range,false
otherwise.
-
tokenizeString
public static List tokenizeString(String str, String token)
A method to chop up a String on another multi-character String and then return the results in a List. If you have a single character String then you should use java.util.StringTokenizer.- Parameters:
str
- The String to chop up.token
- The token to look for. This should be just a plain String, this method doesn't support regular expressions.- Returns:
- A List of all the Strings found, if token is not present then return a single sized List with the str at 0.
-
hasValue
public static boolean hasValue(String v)
Determine whether the specified string contains a value, where having a value means:(string != null) && (!string.equals (""))
- Returns:
true
if the string has a value,false
otherwise.
-
replaceAllStrings
public static String replaceAllStrings(String text, Map map, boolean caseSensitive)
Given a Map of String/String (or Object/Object, they will be converted to Strings before replacement) replace all instances of each one in the specified text.
There is no guarantee about the order in which the replacements will occur, if the ordering is important then you should impose the order by using a sorted map.
The original text will not be affected.- Parameters:
text
- The text to perform the replacements on.map
- The map of String to find in the text and replace with the value of the map (String).caseSensitive
- Indicate whether we should consider case when searching for the strings.- Returns:
- A new String representing the text with all the replacements made. If any of the keys/values in the map are null then we return "".
-
replaceAllStrings
public static String replaceAllStrings(String text, Map map)
Given a Map of String/String (or Object/Object, they will be converted to Strings before replacement) replace all instances of each one in the specified text.
There is no guarantee about the order in which the replacements will occur, if the ordering is important then you should impose the order by using a sorted map.
The original text will not be affected.- Parameters:
text
- The text to perform the replacements on.map
- The map of String to find in the text and replace with the value of the map (String).- Returns:
- A new String representing the text with all the replacements made. If any of the keys/values in the map are null then we return "".
-
replaceString
public static String replaceString(String text, String str, String replace, boolean caseSensitive) throws NullPointerException
Replace all instances of the specified string in the text given. This will not recursively replace, if it finds something to replace it replaces and then goes from the place it found last.It should be noted that we take copies of text and str prior to performing the replacements.
- Parameters:
text
- The text to perform the replacement on.str
- The string to find in the text.replace
- The string to replace "str" with.caseSensitive
- Indicate whether we should consider case when searching for the string.- Returns:
- A new String representing the replaced text (if any were made).
- Throws:
NullPointerExceptio
- If any of the arguments are null indicating which one is at fault.NullPointerException
-
replaceString
public static String replaceString(String text, String str, String replace) throws NullPointerException
Replace all instances of the specified string in the text given. This will not recursively replace, if it finds something to replace it replaces and then goes from the place it found last.- Parameters:
text
- The text to perform the replacement on.str
- The string to find in the text.replace
- The string to replace "str" with.- Returns:
- A new String representing the replaced text (if any were made).
- Throws:
NullPointerException
- If any of the arguments are null indicating which one is at fault.
-
areAllCharsInRange
public static boolean areAllCharsInRange(String value, char start, char end)
Indicate whether the String value has ALL of it's characters in the specified range of characters. Make sure that end is "greater" than start. If value is null return false.Note: if you set start == end then you check to see if a String is made up of the same character, not useful all the time but it is sometimes.
- Parameters:
value
- The value to check.start
- The starting character of the range.end
- The end character of the range.- Returns:
- Whether all the characters are in the range.
Return
true
if they are,false
otherwise.
-
-