Difference between revisions of "TextUtils"

From GreenVulcano Wiki
Jump to: navigation, search
Line 3: Line 3:
 
==Following the TextUtils public static methods==
 
==Following the TextUtils public static methods==
 
<syntaxhighlight lang="java5">     
 
<syntaxhighlight lang="java5">     
    /**
+
/**
    * Compile pattern as a regular expression then check if exists a match in text.
+
* Compiles pattern as a regular expression then checks if exists a match in text.
    *  
+
*  
    * @param pattern
+
* @param pattern
    * @param text
+
* @param text
    * @return true if the pattern match the text
+
* @return true if the pattern match the text
    */
+
*/
    public static boolean matches(String pattern, String text)
+
public static boolean matches(String pattern, String text)
  
    /**
+
/**
    * Replaces within input string all the occurrences of the
+
* Replaces within input string all the occurrences of the
    * substring toBeReplaced with occurrences of the substring replacement.
+
* substring toBeReplaced with occurrences of the substring replacement.
    *  
+
*  
    * @param input
+
* @param input
    *        the input String.
+
*        the input String.
    * @param toBeReplaced
+
* @param toBeReplaced
    *        the substring to be replaced within input string.
+
*        the substring to be replaced within input string.
    * @param replacement
+
* @param replacement
    *        the string used to replace substring toBeReplaced within input string.
+
*        the string used to replace substring toBeReplaced within input string.
    * @return the input string, with all occurrences of the substring
+
* @return the input string, with all occurrences of the substring
    *        toBeReplaced replaced by occurrences of the substring replacement.
+
*        toBeReplaced replaced by occurrences of the substring replacement.
    */
+
*/
    public static String replaceSubstring(String input, String toBeReplaced, String replacement);
+
public static String replaceSubstring(String input, String toBeReplaced, String replacement);
 
    
 
    
    /**
+
/**
    * Replaces placeholders value in the input string.
+
* Replaces placeholders value in the input string.
    *
+
*
    * @param input
+
* @param input
    *        the input text
+
*        the input text
    * @param phPrefix
+
* @param phPrefix
    *        the placeholder prefix
+
*        the placeholder prefix
    * @param phSuffix
+
* @param phSuffix
    *        the placeholder suffix
+
*        the placeholder suffix
    * @param phValues
+
* @param phValues
    *        the placeholder values
+
*        the placeholder values
    * @return the string with placeholders replaced
+
* @return the string with placeholders replaced
    *  
+
*  
    */
+
*/
    public static String replacePlaceholder(String input, String phPrefix, String phSuffix, Hashtable<String, String> phValues)
+
public static String replacePlaceholder(String input, String phPrefix, String phSuffix, Hashtable<String, String> phValues)
  
    /**
+
/**
    * This method tokenizes a given string using another given string as
+
* This method tokenizes a given string using another given string as
    * separator and returns a List containing found tokens. The
+
* separator and returns a List containing found tokens. The
    * returned List is NEVER null (it may have zero elements, anyway).
+
* returned List is NEVER null (it may have zero elements, anyway).
    *  
+
*  
    * @param theString
+
* @param theString
    *        the string to be tokenized.
+
*        the string to be tokenized.
    * @param separatorString  
+
* @param separatorString  
    *        the string separator between tokens.
+
*        the string separator between tokens.
    * @return a List containing found tokens.
+
* @return a List containing found tokens.
    */
+
*/
    public static List<String> splitByStringSeparator(String theString, String separatorString)
+
public static List<String> splitByStringSeparator(String theString, String separatorString)
  
    /**
+
/**
    * Replaces JavaScript invalid chars within input String with the
+
* Replaces JavaScript invalid chars within input String with the
    * corresponding escapes.
+
* corresponding escapes.
    *  
+
*  
    * @param input the input String.
+
* @param input the input String.
    * @return the input string, with JS invalid chars replaced by the
+
* @return the input string, with JS invalid chars replaced by the
    *        corresponding escapes.
+
*        corresponding escapes.
    */
+
*/
    public static String replaceJSInvalidChars(String input)
+
public static String replaceJSInvalidChars(String input)
  
    /**
+
/**
    * Replaces SQL invalid chars within input String with the
+
* Replaces SQL invalid chars within input String with the
    * corresponding escapes.
+
* corresponding escapes.
    *  
+
*  
    * @param input
+
* @param input
    *        the input String.
+
*        the input String.
    * @return the input string, with SQL invalid chars replaced by the
+
* @return the input string, with SQL invalid chars replaced by the
    *        corresponding escapes.
+
*        corresponding escapes.
    */
+
*/
    public static String replaceSQLInvalidChars(String input)
+
public static String replaceSQLInvalidChars(String input)
  
    /**
+
/**
    * Dumps the content of a byte array as chars
+
* Dumps the content of a byte array as chars
    *  
+
*  
    * @param arr
+
* @param arr
    *        a byte array
+
*        a byte array
    * @return A representation of the given byte array as a sequence of chars
+
* @return A representation of the given byte array as a sequence of chars
    */
+
*/
    public static String dumpByteArrayAsChars(byte[] arr)
+
public static String dumpByteArrayAsChars(byte[] arr)
  
    /**
+
/**
    * Generate random chars string of given length.
+
* Generates random chars string of given length.
    *  
+
*  
    * @param length
+
* @param length
    * @return a random string of given length.
+
* @return a random string of given length.
    */
+
*/
    public static String generateRandomString(int length)
+
public static String generateRandomString(int length)
  
    /**
+
/**
    * Return a String representation of the given throwable stack-trace.
+
* Returns a String representation of the given throwable stack-trace.
    *
+
*
    * @param throwable
+
* @param throwable
    * @return riturn the stack-trace
+
* @return the stack-trace
    */
+
*/
    public static String getStackTrace(Throwable throwable)
+
public static String getStackTrace(Throwable throwable)
  
    /**
+
/**
    * Convert the following definitions to the corresponding EOL char/s:
+
* Converts the following definitions to the corresponding EOL char/s:
    * - \n or LF : line feed
+
* - \n or LF : line feed
    * - \r or CR : carriage return
+
* - \r or CR : carriage return
    * - \r\n or CR-LF : carriage return and line feed
+
* - \r\n or CR-LF : carriage return and line feed
    * - native : OS native EOL
+
* - native : OS native EOL
    *  
+
*  
    * @param def
+
* @param def
    *        the definition to convert
+
*        the definition to convert
    * @return the EOL char/s or null
+
* @return the EOL char/s or null
    */
+
*/
    public static String getEOL(String def)
+
public static String getEOL(String def)
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 13:54, 21 December 2012

Class FQN: it.greenvulcano.util.txt.TextUtils

Following the TextUtils public static methods

    
/**
* Compiles pattern as a regular expression then checks if exists a match in text.
* 
* @param pattern
* @param text
* @return true if the pattern match the text
*/
public static boolean matches(String pattern, String text)

/**
* Replaces within input string all the occurrences of the
* substring toBeReplaced with occurrences of the substring replacement.
* 
* @param input
*        the input String.
* @param toBeReplaced
*        the substring to be replaced within input string.
* @param replacement
*        the string used to replace substring toBeReplaced within input string.
* @return the input string, with all occurrences of the substring
*         toBeReplaced replaced by occurrences of the substring replacement.
*/
public static String replaceSubstring(String input, String toBeReplaced, String replacement);
   
/**
* Replaces placeholders value in the input string.
*
* @param input
*        the input text
* @param phPrefix
*        the placeholder prefix
* @param phSuffix
*        the placeholder suffix
* @param phValues
*        the placeholder values
* @return the string with placeholders replaced
* 
*/
public static String replacePlaceholder(String input, String phPrefix, String phSuffix, Hashtable<String, String> phValues)

/**
* This method tokenizes a given string using another given string as
* separator and returns a List containing found tokens. The
* returned List is NEVER null (it may have zero elements, anyway).
* 
* @param theString
*        the string to be tokenized.
* @param separatorString 
*        the string separator between tokens.
* @return a List containing found tokens.
*/
public static List<String> splitByStringSeparator(String theString, String separatorString)

/**
* Replaces JavaScript invalid chars within input String with the
* corresponding escapes.
* 
* @param input the input String.
* @return the input string, with JS invalid chars replaced by the
*         corresponding escapes.
*/
public static String replaceJSInvalidChars(String input)

/**
* Replaces SQL invalid chars within input String with the
* corresponding escapes.
* 
* @param input
*        the input String.
* @return the input string, with SQL invalid chars replaced by the
*         corresponding escapes.
*/
public static String replaceSQLInvalidChars(String input)

/**
* Dumps the content of a byte array as chars
* 
* @param arr
*        a byte array
* @return A representation of the given byte array as a sequence of chars
*/
public static String dumpByteArrayAsChars(byte[] arr)

/**
* Generates random chars string of given length.
* 
* @param length
* @return a random string of given length.
*/
public static String generateRandomString(int length)

/**
* Returns a String representation of the given throwable stack-trace.
*
* @param throwable
* @return the stack-trace
*/
public static String getStackTrace(Throwable throwable)

/**
* Converts the following definitions to the corresponding EOL char/s:
* - \n or LF : line feed
* - \r or CR : carriage return
* - \r\n or CR-LF : carriage return and line feed
* - native : OS native EOL
* 
* @param def
*        the definition to convert
* @return the EOL char/s or null
*/
public static String getEOL(String def)


Follows some usage examples:

/* We assume that #map contains the following mappings:
 * prop1 -> value1
 * prop2 -> value2
 */
#input = "text text prop{{prop1}} text text prop{{prop2}}",
#value = @it.greenvulcano.util.txt.TextUtils@replacePlaceholder(#input, "prop{{", "}}", #map)

// #value contains: "text text value1 text text value2"