pt.tumba.cage
Class StringUtils

java.lang.Object
  extended by pt.tumba.cage.StringUtils

public class StringUtils
extends java.lang.Object

A collection of String handling utility methods.

Some of the methods available in this class have equivalents in the java.lang.String class. However, the implementations provided here are a lot faster, since they do not deal with character internationalization issues.

Author:
Bruno Martins

Method Summary
static java.lang.String capitalizate(java.lang.String str, boolean accents)
          Capitalizates a given String.
static int count(java.lang.String str, char c)
          Counts the occurrence of the given char in a String.
static StringUtils getInstance()
          Return the single instance of this class.
static char[] getSpecialChars()
          Return an array with all the valid accented characters.
static java.lang.String invertString(java.lang.String s)
          Reverse a given String.
static boolean isAccent(char chr)
          Checks if a given character has diacritics.
static boolean isAlphaNumeric(char c)
          Tests whether a given character is alphabetic, numeric or the hyphen character.
static boolean isCapitalizated(java.lang.String str)
          Checks if a given String is capitalizated.
static boolean isUpperCase(char chr)
          Checks if a given character is uppercase.
static boolean isVowel(java.lang.String in, int at)
          Checks if the character at a given position of a given string is a vowel.
static boolean isVowel(java.lang.String in, int at, int length)
          Checks if the character at a given position of a given string is a vowel The Y character is also considered.
static int matchStrings(java.lang.String a, java.lang.String b)
          Matches two strings.
static java.lang.String replace(java.lang.String source, java.lang.String search, java.lang.String replace)
          Returns a new string resulting from replacing all occurrences of the String search in the String source, with the string replace.
static char replaceAccent(char chr)
          Replaces accented characters with their variations without the diacritics.
static java.lang.String separateNumberWithDots(java.lang.String n)
          Takes a numeric string and separates groups of 3 characters with a '.' character.
static java.lang.String separateNumberWithDots(java.lang.String n, int s)
          Takes a numeric string and separates groups of "n" characters with a '.' character.
static java.lang.String toLowerCase(java.lang.String str, boolean accents)
          Converts all of the characters in a given String to lower case.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isAlphaNumeric

public static boolean isAlphaNumeric(char c)
Tests whether a given character is alphabetic, numeric or the hyphen character.

Parameters:
c - The character to be tested.
Returns:
whether the given character is alphameric or not.

count

public static int count(java.lang.String str,
                        char c)
Counts the occurrence of the given char in a String.

Parameters:
str - The string to be tested.
c - the char to be counted.
Returns:
the frequency of occurrence for the character in the String.

matchStrings

public static int matchStrings(java.lang.String a,
                               java.lang.String b)
Matches two strings.

Parameters:
a - The first string.
b - The second string.
Returns:
the index where the two strings stop matching starting from 0.

getInstance

public static StringUtils getInstance()
Return the single instance of this class.

Returns:
An instance of StringUtils.

invertString

public static java.lang.String invertString(java.lang.String s)
Reverse a given String.

Parameters:
s - The String to reverse.
Returns:
The reversed string.

replace

public static java.lang.String replace(java.lang.String source,
                                       java.lang.String search,
                                       java.lang.String replace)
Returns a new string resulting from replacing all occurrences of the String search in the String source, with the string replace.

Parameters:
source - The original String.
search - The string to be replaces.
replace - The replacement String.
Returns:
The resulting String.

replaceAccent

public static char replaceAccent(char chr)
Replaces accented characters with their variations without the diacritics. TODO: add more non-Portuguese diacritic characters.

Parameters:
chr - the character to check.
Returns:
The character without the diacritic.

isAccent

public static boolean isAccent(char chr)
Checks if a given character has diacritics. For instance, isAccent('a') would return false, whereas isAccent('รก') would return true.

Parameters:
chr - the char to check.
Returns:
true if the character has a diacritic and false otherwise.

isUpperCase

public static boolean isUpperCase(char chr)
Checks if a given character is uppercase. For instance, isUpperCase('a') would return false, whereas isUpperCase('A') would return true.

Parameters:
chr - the char to check.
Returns:
true if the character is uppercase and false otherwise.

separateNumberWithDots

public static java.lang.String separateNumberWithDots(java.lang.String n)
Takes a numeric string and separates groups of 3 characters with a '.' character. For instance separateNumberWithDots(n) would return "1.000".

Parameters:
n - A numeric String.
Returns:
The resulting String.

separateNumberWithDots

public static java.lang.String separateNumberWithDots(java.lang.String n,
                                                      int s)
Takes a numeric string and separates groups of "n" characters with a '.' character. For instance separateNumberWithDots(n,3) would return "1.000"

Parameters:
n - A numeric String.
s - The number of characters to group.
Returns:
The resulting String.

toLowerCase

public static java.lang.String toLowerCase(java.lang.String str,
                                           boolean accents)
Converts all of the characters in a given String to lower case.

Parameters:
str - A String.
accents - if true, then besides converting the string to lower case accented characters are also replaces with their versions without the diacritics.
Returns:
The resulting String.

getSpecialChars

public static char[] getSpecialChars()
Return an array with all the valid accented characters. TODO: add more non-Portuguese diacritic characters.

Returns:
An array with all the valid accented characters.

isVowel

public static final boolean isVowel(java.lang.String in,
                                    int at)
Checks if the character at a given position of a given string is a vowel. The Y character is also considered. TODO: Should portuguese accented characters be considered vowels?

Parameters:
in - A String.
at - The position in the String.
Returns:
true if the the character at position at of the string in is a vowel and false otherwise.

isVowel

public static boolean isVowel(java.lang.String in,
                              int at,
                              int length)
Checks if the character at a given position of a given string is a vowel The Y character is also considered. TODO: Should portuguese accented characters be considered vowels?

Parameters:
in - A String.
at - The position in the String.
length - The maximum lengh of the String to check.
Returns:
true if the the character at position at of the string in is a vowel and false otherwise.

isCapitalizated

public static boolean isCapitalizated(java.lang.String str)
Checks if a given String is capitalizated.

Parameters:
str - A String.
Returns:
true if the given String is capitalizated and false otherwise.

capitalizate

public static java.lang.String capitalizate(java.lang.String str,
                                            boolean accents)
Capitalizates a given String.

Parameters:
str - A String.
Returns:
The capitalizated String.