StringUtil

Kind of class:public class
Package:org.casalib.util
Inherits from:none
Version:03/29/10
Author:Aaron Clinger, Mike Creighton, David Nelson, Jon Adams
Classpath:org.casalib.util.StringUtil
File last modified:Monday, 29 March 2010, 08:27:59
Utilities for manipulating and searching Strings.

Summary


Constants
  • WHITESPACE : String
    • Whitespace characters (space, tab, new line and return).
Class properties
  • SMALL_WORDS : Array
    • The default list of small/short words to be used with toTitleCase.
Class methods
  • truncate (source:String, trailing:uint, leading:uint = 0, separator:String = "") : String
    • Returns a shortened String.
  • toTitleCase (source:String, lowerCaseSmallWords:Boolean = true) : String
    • Transforms source String to title case.
  • uuid : String
    • Creates an "universally unique" identifier (RFC 4122, version 4).
  • createRandomIdentifier (length:uint, radix:uint = 61) : String
    • Creates a random identifier of a specified length and complexity.
  • autoLink (source:String, window:String = "_blank", className:String = null) : String
    • Detects URLs in a String and wraps them in a link.
  • htmlEncode (source:String) : String
    • Converts all applicable characters to HTML entities.
  • htmlDecode (source:String) : String
    • Converts all HTML entities to their applicable characters.
  • isPunctuation (source:String, allowSpaces:Boolean = true) : Boolean
    • Determines if String is only comprised of punctuation characters (any character other than the letters or numbers).
  • isUpperCase (source:String) : Boolean
    • Determines if String is only comprised of upper case letters.
  • isLowerCase (source:String) : Boolean
    • Determines if String is only comprised of lower case letters.
  • isNumber (source:String) : Boolean
    • Determines if String is only comprised of numbers.
  • indexOfUpperCase (source:String, startIndex:uint = 0) : int
    • Searches the String for an occurrence of an upper case letter.
  • indexOfLowerCase (source:String, startIndex:uint = 0) : int
    • Searches the String for an occurrence of a lower case letter.
  • getNumbersFromString (source:String) : String
    • Returns all the numeric characters from a String.
  • getLettersFromString (source:String) : String
    • Returns all the letter characters from a String.
  • contains (source:String, search:String) : uint
    • Determines if String contains search String.
  • trimLeft (source:String, removeChars:String = StringUtil) : String
    • Strips whitespace (or other characters) from the beginning of a String.
  • trimRight (source:String, removeChars:String = StringUtil) : String
    • Strips whitespace (or other characters) from the end of a String.
  • trim (source:String, removeChars:String = StringUtil) : String
    • Strips whitespace (or other characters) from the beginning and end of a String.
  • removeExtraSpaces (source:String) : String
    • Removes additional spaces from String.
  • removeWhitespace (source:String) : String
    • Removes tabs, linefeeds, carriage returns and spaces from String.
  • remove (source:String, remove:String) : String
    • Removes characters from a source String.
  • replace (source:String, remove:String, replace:String) : String
    • Replaces target characters with new characters.
  • removeAt (source:String, position:int) : String
    • Removes a character at a specific index.
  • replaceAt (source:String, position:int, replace:String) : String
    • Replaces a character at a specific index with new characters.
  • addAt (source:String, position:int, addition:String) : String
    • Adds characters at a specific index.
  • getWordCount (source:String) : uint
    • Counts the number of words in a String.
  • getUniqueCharacters (source:String) : String
    • Extracts all the unique characters from a source String.

Constants

WHITESPACE

public static const WHITESPACE:String = " \t\n\r"
(read)

Whitespace characters (space, tab, new line and return).

Class properties

SMALL_WORDS

public static SMALL_WORDS:Array = new Array("a", "an", "and", "as", "at", "but", "by", "en", "for", "if", "is", "in", "of", "on", "or", "the", "to", "v", "via", "vs")
(read,write)

The default list of small/short words to be used with toTitleCase.

Class methods

addAt

public static function addAt (
source:String, position:int, addition:String) : String

Adds characters at a specific index.
Parameters:
source :
String to add characters to.
position:
Position in which to add characters.
addition:
String to add.
Returns:
  • String with characters added.

contains

public static function contains (
source:String, search:String) : uint

Determines if String contains search String.
Parameters:
source:
String to search in.
search:
String to search for.
Returns:
  • Returns the frequency of the search term found in source String.

createRandomIdentifier

public static function createRandomIdentifier (
length:uint, radix:uint = 61) : String

Creates a random identifier of a specified length and complexity.
Parameters:
length:
The character length of the random identifier.
radix :
The number of unique/allowed values for each character (61 is the maximum complexity).
Returns:
  • Returns a random identifier.
Usage note:
  • For a case-insensitive identifier pass in a max radix of 35, for a numberic identifier pass in a max radix of 9.

getLettersFromString

public static function getLettersFromString (
source:String) : String

Returns all the letter characters from a String.
Parameters:
source:
String to return letters from.
Returns:
  • String containing only letters.

getNumbersFromString

public static function getNumbersFromString (
source:String) : String

Returns all the numeric characters from a String.
Parameters:
source:
String to return numbers from.
Returns:
  • String containing only numbers.

getUniqueCharacters

public static function getUniqueCharacters (
source:String) : String

Extracts all the unique characters from a source String.
Parameters:
source:
String to find unique characters within.
Returns:
  • String containing unique characters from source String.

getWordCount

public static function getWordCount (
source:String) : uint

Counts the number of words in a String.
Parameters:
source:
String in which to count words.
Returns:
  • The amount of words.

htmlDecode

public static function htmlDecode (
source:String) : String

Converts all HTML entities to their applicable characters.
Parameters:
source:
String to convert.
Returns:
  • Returns the converted string.

htmlEncode

public static function htmlEncode (
source:String) : String

Converts all applicable characters to HTML entities.
Parameters:
source:
String to convert.
Returns:
  • Returns the converted string.

indexOfLowerCase

public static function indexOfLowerCase (
source:String, startIndex:uint = 0) : int

Searches the String for an occurrence of a lower case letter.
Parameters:
source:
String to search for a lower case letter.
Returns:
  • The index of the first occurrence of a lower case letter or -1.

indexOfUpperCase

public static function indexOfUpperCase (
source:String, startIndex:uint = 0) : int

Searches the String for an occurrence of an upper case letter.
Parameters:
source:
String to search for a upper case letter.
Returns:
  • The index of the first occurrence of a upper case letter or -1.

isLowerCase

public static function isLowerCase (
source:String) : Boolean

Determines if String is only comprised of lower case letters.
Parameters:
source:
String to check.
Returns:
  • Returns true if String is only lower case characters; otherwise false.
Usage note:
  • This function counts numbers, spaces, punctuation and special characters as lower case.

isNumber

public static function isNumber (
source:String) : Boolean

Determines if String is only comprised of numbers.
Parameters:
source:
String to check.
Returns:
  • Returns true if String is a number; otherwise false.

isPunctuation

public static function isPunctuation (
source:String, allowSpaces:Boolean = true) : Boolean

Determines if String is only comprised of punctuation characters (any character other than the letters or numbers).
Parameters:
source :
String to check.
allowSpaces:
Indicates to count spaces as punctuation true, or not to false.
Returns:
  • Returns true if String is only punctuation; otherwise false.

isUpperCase

public static function isUpperCase (
source:String) : Boolean

Determines if String is only comprised of upper case letters.
Parameters:
source:
String to check.
Returns:
  • Returns true if String is only upper case characters; otherwise false.
Usage note:
  • This function counts numbers, spaces, punctuation and special characters as upper case.

remove

public static function remove (
source:String, remove:String) : String

Removes characters from a source String.
Parameters:
source:
String to remove characters from.
remove:
String describing characters to remove.
Returns:
  • String with characters removed.

removeAt

public static function removeAt (
source:String, position:int) : String

Removes a character at a specific index.
Parameters:
source :
String to remove character from.
position:
Position of character to remove.
Returns:
  • String with character removed.

removeExtraSpaces

public static function removeExtraSpaces (
source:String) : String

Removes additional spaces from String.
Parameters:
source:
String to remove extra spaces from.
Returns:
  • String with additional spaces removed.

removeWhitespace

public static function removeWhitespace (
source:String) : String

Removes tabs, linefeeds, carriage returns and spaces from String.
Parameters:
source:
String to remove whitespace from.
Returns:
  • String with whitespace removed.

replace

public static function replace (
source:String, remove:String, replace:String) : String

Replaces target characters with new characters.
Parameters:
source :
String to replace characters from.
remove :
String describing characters to remove.
replace:
String to replace removed characters.
Returns:
  • String with characters replaced.

replaceAt

public static function replaceAt (
source:String, position:int, replace:String) : String

Replaces a character at a specific index with new characters.
Parameters:
source :
String to replace characters from.
position:
Position of character to replace.
replace :
String to replace removed character.
Returns:
  • String with character replaced.

toTitleCase

public static function toTitleCase (
source:String, lowerCaseSmallWords:Boolean = true) : String

Transforms source String to title case.
Parameters:
source :
String to return as title cased.
lowerCaseSmallWords:
Indicates to make small words lower case true, or to capitalized small words false.
Returns:
  • String with capitalized words.

trim

public static function trim (
source:String, removeChars:String = StringUtil) : String

Strips whitespace (or other characters) from the beginning and end of a String.
Parameters:
source :
String to remove characters from.
removeChars:
Characters to strip (case sensitive). Defaults to whitespace characters.
Returns:
  • String with characters removed.

trimLeft

public static function trimLeft (
source:String, removeChars:String = StringUtil) : String

Strips whitespace (or other characters) from the beginning of a String.
Parameters:
source :
String to remove characters from.
removeChars:
Characters to strip (case sensitive). Defaults to whitespace characters.
Returns:
  • String with characters removed.

trimRight

public static function trimRight (
source:String, removeChars:String = StringUtil) : String

Strips whitespace (or other characters) from the end of a String.
Parameters:
source :
String to remove characters from.
removeChars:
Characters to strip (case sensitive). Defaults to whitespace characters.
Returns:
  • String with characters removed.

truncate

public static function truncate (
source:String, trailing:uint, leading:uint = 0, separator:String = "") : String

Returns a shortened String.
Parameters:
source :
String to shorten.
trailing :
The number of characters to remove from the end of the String.
leading :
The number of characters to remove from the begining of the String.
separator:
Characters to seperate the begining and the end of the String.
Returns:
  • The shortened String.
Example:
  • trace(StringUtil.truncate('Mississippi', 2, 3, '...')); // Traces "Mis...pi"

uuid

public static function uuid (
) : String

Creates an "universally unique" identifier (RFC 4122, version 4).
Returns:
  • Returns an UUID.