Dashboard Builder Commons 6.2.0.CR3

org.jboss.dashboard.commons.text
Class StringUtil

java.lang.Object
  extended by org.jboss.dashboard.commons.text.StringUtil

public final class StringUtil
extends Object

Utilities to work with strings.


Field Summary
static int CENTER_JUSTIFY
          Center text.
static String[] JAVA_KEYWORDS
           
static int LEFT_JUSTIFY
          Justify text to the left side.
static String NON_TILDED_CHARS
           
static int RIGHT_JUSTIFY
          Justify text to the right side.
static String SPECIAL_CHARS
           
static String TILDED_CHARS
           
 
Method Summary
static String chomp(char[] charArray)
          Given a character array, this method constructs a new string without trailing carriage return or newline characters.
static String[] cutString(String text, FontMetrics fontMetrics, int maxWidth)
          It returns an array of subchains of maximum length in pixels.
static String eliminateWordsShorterThan(String str, int size)
          Delete all the words of a string smaller that a given size, and the extra blank characters
static String escapeQuotes(String str)
           
static String fill(String str, char ch, int size)
          Fill a string with chars, in order to grow to a given size.
static String fill(String str, String strFill, int size)
          Fill a string repeating other string, in order to grow to a given size.
static String fill(String str, String strFill, int size, boolean fillRight)
          Fill a string repeating other string, in order to grow to a given size.
static String filterSQLString(String str)
          Replace all the ocurrences of the char ' with '', and add a ' at the start and the end of the string.
static String findFirst(String str, String find)
          Searches into a String for the prefix before the specified substring.
static String findLast(String str, String find)
          Search into a String for the suffix after the last occurrence of the specified substring.
static String findRest(String str, String find)
          Search into a String for the suffix after the first occurrence of the specified substring.
static String firstLow(String str)
          Lower case the first character in a String.
static String firstUp(String str)
          Upper case the first character in a String.
static String fixUnicodeCharacters(String name)
          Replace special characters with simple characters
static String getStringFromTokens(Vector vector, String delimiter)
          Return a String who contains the strings in the Vector vector separated by delimiter.
static Vector getTokensFromString(String str)
          Return a vector with str divided in fragments.
static Vector getTokensFromString(String str, String delimiter)
          Return a vector with str divided in fragments, separated by delimiter.
static String indentParagraph(String textToModify, int indentSize)
          Insert "indentSize" spaces before every line of textToModify.
static String indentParagraph(String textBefore, String textToModify)
          Deprecated. Use indentParagraph(String,int) instead
static int indexOf(String str, int fromIndex, StringBuffer buf)
          Returns the position of the first ocurrence of the substring into the stringbuffer, starting at fromIndex, or -1 if does not ocurr.
static int indexOf(String str, StringBuffer buf)
          Returns the position of the first ocurrence of the substring into the stringbuffer, or -1 if does not ocurr.
static boolean isJavaIdentifier(String str)
          Checks if the specified string is a valid java identifier.
static boolean isJavaKeyword(String name)
          Returns true if the given String is a Java keyword which will cause a problem when used as a variable name.
static boolean isJavaPackage(String name)
          Checks if the given package name is valid or not.
static String justify(String str, int size, int type)
          Justify a string.
static String lowerFirstLetter(String str)
          Converts the first letter to lower case.
static String lowerFirstUp(String str)
          Upper case the first character in a String, lower the rest of the string.
static void main(String[] args)
           
static String makeSQLString(String str)
          Convert a string to SQL sentence string.
static int ocurrencesOf(String str, char chr)
          Get the number of ocurrences of a character into a string
static String parseASCIITemplate(String template, Map params)
          Parses an ASCII template that contains parameters like [#param#] and replace them with the values associated in params.
static String parseASCIITemplate(String template, Map params, String leftMark, String rightMark)
           
static String parseASCIITemplate(String template, Map params, String leftMark, String rightMark, boolean nullifyNotFound)
           
static String removeAll(String origStr, String removeStr)
          Remove all the ocurrences of a substring from a string
static String removeAll(String str, String[] substr)
          Remove all the ocurrences of a group of substring from a string
static String removeFirst(String origStr, String removeStr)
          Remove the first ocurrence of a substring from a string
static String repeat(String str, int num)
          Repeat a String n times.
static String replace(String origStr, char oldChar, String newStr)
          Deprecated. replaced by replaceAll()
static String replace(String origStr, String oldStr, String newStr)
          Deprecated. replaced by replaceAll()
static String replaceAll(String origStr, char oldChar, String newStr)
          Replaces a character of a String with characters in the specified new substring.
static String replaceAll(String str, String[] in, String[] out)
          Replaces the characters in all substrings of a String with characters in the specified array of substrings.
static String replaceAll(String str, String[] in, String[] out, String beginDelim, String endDelim)
          Returns a String who replace Strings include into str, which are into delimiters and known in in, to corresponding String from out.
static String replaceAll(String origStr, String oldStr, String newStr)
          Replaces the characters in a substring of a String with characters in the specified new substring.
static String replaceAll(String str, Vector in, Vector out, String beginDelim, String endDelim)
          Returns a String who replace Strings include into str, which are into delimiters and known in in, to corresponding String from out.
static String replaceFirst(String origStr, String oldStr, String newStr)
          Replacing first ocurrence of oldStr in origStr with newStr
static Map replaceParamsValues(Map params, String oldSubstring, String newSubstring)
          Replace all the occurences of oldSubstring with newSubstring in the values of a parameter mapping structure
static String replaceUnicodeCharacters(String str)
          Convert special characters in unicode characters
static String soundTrex(String original)
          Soundtrex algorithm.
static String substring(String str, int beginIndex, int endIndex)
          Return a new String that is a substring of this string.
static String subString(String str, int beginIndex, int endIndex)
          Deprecated. Use substring instead
static String toJavaClassName(String name)
          Get a valid java class name for the given string.
static String toJavaFieldName(String name)
          Get a valid java filed name for the given string.
static String toJavaIdentifier(String str)
          Converts the given string to a Java valid identifier.
static String toJavaPackagePath(String packageName)
          Converts the given Package name to it's corresponding Path.
static String trimLowerCase(String str)
          Remove white space from both ends of this string and converts all of the characters in this String to lower case.
static String trimUpperCase(String str)
          Remove white space from both ends of this string and converts all of the characters in this String to upper case.
static String upperFirstLetter(String str)
          Converts the first letter to upper case.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LEFT_JUSTIFY

public static final int LEFT_JUSTIFY
Justify text to the left side.

See Also:
Constant Field Values

RIGHT_JUSTIFY

public static final int RIGHT_JUSTIFY
Justify text to the right side.

See Also:
Constant Field Values

CENTER_JUSTIFY

public static final int CENTER_JUSTIFY
Center text.

See Also:
Constant Field Values

SPECIAL_CHARS

public static final String SPECIAL_CHARS

TILDED_CHARS

public static final String TILDED_CHARS

NON_TILDED_CHARS

public static final String NON_TILDED_CHARS
See Also:
Constant Field Values

JAVA_KEYWORDS

public static final String[] JAVA_KEYWORDS
Method Detail

replace

public static String replace(String origStr,
                             char oldChar,
                             String newStr)
Deprecated. replaced by replaceAll()

Replaces a character of a String with characters in the specified new substring.

Parameters:
origStr - string that contains the replaceable substring
oldChar - character to search in the orig String
newStr - substring of the new characters
Returns:
if the parameters are correct returns the new string; otherwise if origStr parameter is null returns null. If newstr parameter is null returns origStr parameter

replaceAll

public static String replaceAll(String origStr,
                                char oldChar,
                                String newStr)
Replaces a character of a String with characters in the specified new substring.

Parameters:
origStr - string that contains the replaceable substring
oldChar - character to search in the orig String
newStr - substring of the new characters
Returns:
if the parameters are correct returns the new string; otherwise if origStr parameter is null returns null. If newstr parameter is null returns origStr parameter

replaceFirst

public static String replaceFirst(String origStr,
                                  String oldStr,
                                  String newStr)
Replacing first ocurrence of oldStr in origStr with newStr

Parameters:
origStr - original string
oldStr - substring to search in the orig String
newStr - new substring
Returns:
a new string with the first ocurrence of oldStr replaced by newStr

replace

public static String replace(String origStr,
                             String oldStr,
                             String newStr)
Deprecated. replaced by replaceAll()

Replaces the characters in a substring of a String with characters in the specified new substring.

Parameters:
origStr - original string
oldStr - substring to search in the orig String
newStr - new substring
Returns:
if the parameters are correct returns the new string; otherwise if orig parameter is null returns null. If str or newstr parameters are null returns orig parameter

replaceAll

public static String replaceAll(String origStr,
                                String oldStr,
                                String newStr)
Replaces the characters in a substring of a String with characters in the specified new substring.

Parameters:
origStr - original string
oldStr - substring to search in the orig String
newStr - new substring
Returns:
if the parameters are correct returns the new string; otherwise if orig parameter is null returns null. If str or newstr parameters are null returns orig parameter

removeFirst

public static String removeFirst(String origStr,
                                 String removeStr)
Remove the first ocurrence of a substring from a string

Parameters:
origStr - original string
removeStr - substring to remove from the orig String
Returns:
an string without the first ocurrence of the substring passed as a parameter

removeAll

public static String removeAll(String origStr,
                               String removeStr)
Remove all the ocurrences of a substring from a string

Parameters:
origStr - original string
removeStr - substring to remove from the orig String
Returns:
an string without all the ocurrences of the substring passed as a parameter

replaceParamsValues

public static Map replaceParamsValues(Map params,
                                      String oldSubstring,
                                      String newSubstring)
Replace all the occurences of oldSubstring with newSubstring in the values of a parameter mapping structure

Parameters:
params - the parameters mapping structure that contains pairs of attr -> value
oldSubstring - the string to replace
newSubstring - the new string
Returns:
new mapping structure (the original structure isn't changed). The values than aren't String are unchanged

parseASCIITemplate

public static String parseASCIITemplate(String template,
                                        Map params)
Parses an ASCII template that contains parameters like [#param#] and replace them with the values associated in params.

The substitution of [#param#] is performed this way:

Parameters:
template - The parametrized template
params - The parameters structure
Returns:
The template with parameters replaced

parseASCIITemplate

public static String parseASCIITemplate(String template,
                                        Map params,
                                        String leftMark,
                                        String rightMark)
Parameters:
template - the parametrized template
params - the parameters structure
leftMark - the left delimiter of the parameters to replace
rightMark - the right delimiter of the parameters to replace
Returns:
The template with parameters replaced
See Also:
parseASCIITemplate(String,Map)

parseASCIITemplate

public static String parseASCIITemplate(String template,
                                        Map params,
                                        String leftMark,
                                        String rightMark,
                                        boolean nullifyNotFound)
Parameters:
template - the parametrized template
params - the parameters structure
leftMark - the left delimiter of the parameters to replace
rightMark - the right delimiter of the parameters to replace
nullifyNotFound - if true, change the not found params to "null"; if false, don't change
Returns:
The template with parameters replaced
See Also:
parseASCIITemplate(String,Map)

indexOf

public static int indexOf(String str,
                          StringBuffer buf)
Returns the position of the first ocurrence of the substring into the stringbuffer, or -1 if does not ocurr.
Returns the smaller i such that

buf.substring(i, str.length()).equals(str),

holds, and -1 if no such i exists.
Assumes str and buf are non-null.

Parameters:
str - substring to find
buf - buffer to search into
Returns:
the position of subStr into buf, or -1 if it does not ocurr

indexOf

public static int indexOf(String str,
                          int fromIndex,
                          StringBuffer buf)
Returns the position of the first ocurrence of the substring into the stringbuffer, starting at fromIndex, or -1 if does not ocurr.
Returns the smaller i greater than or equals to fromIndex such that

buf.substring(i, str.length()).equals(str),

holds, and -1 if no such i exists.

Assumes str and buf are non-null.

Parameters:
str - substring to find
fromIndex - the index to start the search from
buf - buffer to search into
Returns:
the position of subStr into buf, or -1 if it does not ocurr

repeat

public static String repeat(String str,
                            int num)
Repeat a String n times.

Parameters:
str - String to repeat
num - number times to repeat
Returns:
a String who repeats str, num times.

firstUp

public static String firstUp(String str)
Upper case the first character in a String.

Parameters:
str - String to manipulate
Returns:
the String str with first char in UpperCase and the rest like the original String.

firstLow

public static String firstLow(String str)
Lower case the first character in a String.

Parameters:
str - String to manipulate
Returns:
the String str with first char in LowerCase and the rest like the original String.

lowerFirstUp

public static String lowerFirstUp(String str)
Upper case the first character in a String, lower the rest of the string.

Parameters:
str - String to manipulate
Returns:
the String str with first char in UpperCase and the rest in LowerCase.

trimUpperCase

public static String trimUpperCase(String str)
Remove white space from both ends of this string and converts all of the characters in this String to upper case.

Parameters:
str - String to manipulate
Returns:
the String str with trim (without spaces at rigth and at left) and in UpperCase.

trimLowerCase

public static String trimLowerCase(String str)
Remove white space from both ends of this string and converts all of the characters in this String to lower case.

Parameters:
str - String to manipulate
Returns:
the String str with trim (without spaces at rigth and at left) and in LowerCase.

findFirst

public static String findFirst(String str,
                               String find)
Searches into a String for the prefix before the specified substring.

Parameters:
str - String to manipulate
find - String to find in str
Returns:
the first part of String str before the first String find include into. If str doesn't contain find return all str exactly.

findRest

public static String findRest(String str,
                              String find)
Search into a String for the suffix after the first occurrence of the specified substring.

Parameters:
str - String to manipulate
find - String to find in str
Returns:
the rest part of String str after the first String find include into. If str doesn't contain find return all str exactly.

findLast

public static String findLast(String str,
                              String find)
Search into a String for the suffix after the last occurrence of the specified substring.

Parameters:
str - String to manipulate
find - String to find in str
Returns:
the last part of String str after the last String find include into. If str doesn't contain find return all str exactly.

getTokensFromString

public static Vector getTokensFromString(String str,
                                         String delimiter)
Return a vector with str divided in fragments, separated by delimiter. If str doesn't contain delimiter return a vector with one element: str exactly.

Parameters:
str - String to manipulate
delimiter - String who delimiters the Strings into str
Returns:
vector with severals fragmentss of a String as elements of this vector

getTokensFromString

public static Vector getTokensFromString(String str)
Return a vector with str divided in fragments. The fragments are separate by blanks and other usual delimiters: " \t\n\r\f"; the space character, the tab character, the newline character, the carriage-return character and the form-feed character.

Parameters:
str - String to manipulate
Returns:
vector with severals fragments of a String as elements of this vector
See Also:
If str doesn't contain delimiter return a vector with one element: str exactly.

getStringFromTokens

public static String getStringFromTokens(Vector vector,
                                         String delimiter)
Return a String who contains the strings in the Vector vector separated by delimiter.

Parameters:
vector - list of tokens
delimiter - String who delimiters the Strings into str
Returns:
Composed string

subString

public static String subString(String str,
                               int beginIndex,
                               int endIndex)
Deprecated. Use substring instead

See Also:
substring(String,int,int)

substring

public static String substring(String str,
                               int beginIndex,
                               int endIndex)
Return a new String that is a substring of this string. If the endIndex is out of index don�t throw a Exception

Parameters:
str - Cadena de Texto
beginIndex - the begining index, inclusive
endIndex - the ending index, exclusive
Returns:
the specified substring

indentParagraph

public static String indentParagraph(String textBefore,
                                     String textToModify)
Deprecated. Use indentParagraph(String,int) instead

Return a paragraph who insert spaces at the beginning of lines. The amount of spaces that will be inserted is equals to the length of all lines, ended with '\n', in the textBefore string, i.e, if textBefore="in\nden\nstr" seven spaces will be inserted to indent the lines of textToModify string.

Parameters:
textBefore - String where extract the spaces to insert
textToModify - String to modify
Returns:
indented string

indentParagraph

public static String indentParagraph(String textToModify,
                                     int indentSize)
Insert "indentSize" spaces before every line of textToModify. If indentSize is negative or 0, the string is return unchanged

Parameters:
textToModify - string to indent
indentSize - number of spaces to insert
Returns:
a string whith the lines indented

justify

public static String justify(String str,
                             int size,
                             int type)
Justify a string. If the size of the string is smaller than the parameter "size", spaces are appended. If the string is larger than "size", the string is truncated to size.

Parameters:
str - string to justify
size - size of line to justify to
type - type of justify (LEFT_JUSTIFY, RIGHT_JUSTIFY or CENTER_JUSTIFY)
Returns:
justified string

removeAll

public static String removeAll(String str,
                               String[] substr)
Remove all the ocurrences of a group of substring from a string

Parameters:
str - original string
substr - array of substrings to remove from the orig String
Returns:
an string without all the ocurrences of the substring passed as a parameter

replaceAll

public static String replaceAll(String str,
                                String[] in,
                                String[] out)
Replaces the characters in all substrings of a String with characters in the specified array of substrings.

Parameters:
str - original string
in - substrings to search in the orig String
out - array of new substrings
Returns:
if the parameters are correct returns the new string; otherwise if some of the parameters is null returns the original string

replaceAll

public static String replaceAll(String str,
                                Vector in,
                                Vector out,
                                String beginDelim,
                                String endDelim)
Returns a String who replace Strings include into str, which are into delimiters and known in in, to corresponding String from out. If in and out aren't of the same size, the string is returned unchanged.

Parameters:
str - String to manipulate
in - Vector which contains strings to find
out - Vector which contains strings to replace
beginDelim - String who delimiters the begin of the substrings into str
endDelim - String who delimiters the end of the substrings into str
Returns:
the new string

replaceAll

public static String replaceAll(String str,
                                String[] in,
                                String[] out,
                                String beginDelim,
                                String endDelim)
Returns a String who replace Strings include into str, which are into delimiters and known in in, to corresponding String from out.
If in and out aren't of the same size, the string is returned unchanged.
beginDelim and endDelim can't be the same. If they are, a IllegalArgumentException is throw.

Parameters:
str - String to manipulate
in - array of strings to find
out - array of strings to replace
beginDelim - String who delimiters the begin of the substrings into str
endDelim - String who delimiters the end of the substrings into str
Returns:
the new string
Throws:
IllegalArgumentException - if beginDelim and endDelim are equal

eliminateWordsShorterThan

public static String eliminateWordsShorterThan(String str,
                                               int size)
Delete all the words of a string smaller that a given size, and the extra blank characters

Parameters:
str - string to process
size - size limit
Returns:
a string with the words smaller than size deleted, and one space between every word

filterSQLString

public static String filterSQLString(String str)
Replace all the ocurrences of the char ' with '', and add a ' at the start and the end of the string.
For example:
 select * from tabla where nombre = 'Peter' and edad = 25 =>
 

select * from tabla where nombre = ''Peter'' and edad = 25

Parameters:
str - Original String
Returns:
Converted String,

makeSQLString

public static String makeSQLString(String str)
Convert a string to SQL sentence string. Replace all the ocurrences of the char ' with '', and add a ' at the start and the end of the string.
For example:
 select * from tabla where nombre = 'Peter' and edad = 25 =>
 

'select * from tabla where nombre = ''Peter'' and edad = 25'

Parameters:
str - Original String
Returns:
Converted String,

fill

public static String fill(String str,
                          char ch,
                          int size)
Fill a string with chars, in order to grow to a given size. The chars are appended by the right. If size is smaller that the actual size of the string, the string is return unchanged

Parameters:
str - original string
ch - char to fill with
size - final size of the string
Returns:
the string filled

fill

public static String fill(String str,
                          String strFill,
                          int size)
Fill a string repeating other string, in order to grow to a given size. The chars are appended by the right. If size is smaller that the actual size of the string, the string is return unchanged

Parameters:
str - original string
strFill - string to fill with
size - final size of the string
Returns:
the string filled

fill

public static String fill(String str,
                          String strFill,
                          int size,
                          boolean fillRight)
Fill a string repeating other string, in order to grow to a given size. The chars are apended by the right or by the left, according to the param "fillRight". If size is smaller that the actual size of the string, the string is return unchanged

Parameters:
str - original string
strFill - string to fill with
size - final size of the string
fillRight - if true, the chars are appended by the right; if false, by the left
Returns:
the string filled

cutString

public static String[] cutString(String text,
                                 FontMetrics fontMetrics,
                                 int maxWidth)
It returns an array of subchains of maximum length in pixels.

Parameters:
text - original string
fontMetrics - scrren font of text.
maxWidth - maximum width of subchains in pixels.
Returns:
array of subchains of maximum length in pixels.

soundTrex

public static String soundTrex(String original)
Soundtrex algorithm. Soundtrex transform a string into another string that it is phonetic and lexical equivalent to the first string. That is, it extracts the accents, it deletes multiple letters, etc. i.e, The soundtrex of 'ayamonte' is AIAMONTE and the soundtrex of 'allamonte' is AIAMONTE too.
The application of this algorithm is to compare strings but in the comparation it is considered common syntactic errors, i.e, there aren't differences between 'gente' and 'guente'. It can be used at search engines if the indexes are calculated from the soundtrex string.

Parameters:
original - String to analyze
Returns:
Soundtrex of the string parameter

chomp

public static String chomp(char[] charArray)
Given a character array, this method constructs a new string without trailing carriage return or newline characters.

Parameters:
charArray - the character array to construct string from
Returns:
A string without trailing carriage returns or newlines.

ocurrencesOf

public static int ocurrencesOf(String str,
                               char chr)
Get the number of ocurrences of a character into a string

Parameters:
str - string to search into
chr - character to found
Returns:
number of ocurrences of chr into str

lowerFirstLetter

public static String lowerFirstLetter(String str)
Converts the first letter to lower case.


upperFirstLetter

public static String upperFirstLetter(String str)
Converts the first letter to upper case.


toJavaIdentifier

public static String toJavaIdentifier(String str)
Converts the given string to a Java valid identifier.

Parameters:
str - string to process
Returns:
A java based identifier.

isJavaIdentifier

public static boolean isJavaIdentifier(String str)
Checks if the specified string is a valid java identifier.

Parameters:
str - (required).
Returns:
Returns true iff the specified string is a valid java identifier.

isJavaKeyword

public static final boolean isJavaKeyword(String name)
Returns true if the given String is a Java keyword which will cause a problem when used as a variable name.

Parameters:
name - the name to check
Returns:
true if it is a keyword

isJavaPackage

public static boolean isJavaPackage(String name)
Checks if the given package name is valid or not. Empty package names are considered valid!

Parameters:
name - name of package as String with periods
Returns:
true if package name is valid

toJavaPackagePath

public static String toJavaPackagePath(String packageName)
Converts the given Package name to it's corresponding Path. The path will be a relative path.

Parameters:
packageName - the package name to convert
Returns:
a String containing the resulting patch

toJavaClassName

public static String toJavaClassName(String name)
Get a valid java class name for the given string.

Returns:
a name which follows Java naming conventions

toJavaFieldName

public static String toJavaFieldName(String name)
Get a valid java filed name for the given string.


fixUnicodeCharacters

public static String fixUnicodeCharacters(String name)
Replace special characters with simple characters

Parameters:
name - string to replace
Returns:
string with simple characters

replaceUnicodeCharacters

public static String replaceUnicodeCharacters(String str)
Convert special characters in unicode characters

Parameters:
str - string to convert
Returns:
converted string

escapeQuotes

public static String escapeQuotes(String str)

main

public static void main(String[] args)

Dashboard Builder Commons 6.2.0.CR3

Copyright © 2012-2014 JBoss by Red Hat. All Rights Reserved.