Packagecom.threerings.util
Classpublic class StringUtil

Contains useful static function for performing operations on Strings.



Public Methods
 MethodDefined by
  
capitalize(s:String):String
[static] Returns a version of the supplied string with the first letter capitalized.
StringUtil
  
compare(s1:String, s2:String):int
[static] Compares two String values, returning -1, 0, or 1.
StringUtil
  
compareIgnoreCase(s1:String, s2:String):int
[static] Compares two String values, returning -1, 0, or 1.
StringUtil
  
deNull(str:String):String
[static] Return the specified String, or "" if it is null.
StringUtil
  
endsWith(str:String, substr:String):Boolean
[static] Does the specified string end with the specified substring.
StringUtil
  
formatNumber(n:Number):String
[static] Format the specified number, nicely, with commas.
StringUtil
  
fromBytes(bytes:ByteArray):String
[static] Turn the specified byte array, containing only ascii characters, into a String.
StringUtil
  
hashCode(str:String):int
[static] Get a hashCode for the specified String.
StringUtil
  
hexdump(bytes:ByteArray):String
[static] Create line-by-line hexadecimal output with a counter, much like the 'hexdump' Unix utility.
StringUtil
  
hexlate(bytes:ByteArray):String
[static] Generates a string from the supplied bytes that is the hex encoded representation of those byts.
StringUtil
  
isBlank(str:String):Boolean
[static] Is the specified string null, empty, or does it contain only whitespace?
StringUtil
  
isLowerCase(str:String):Boolean
[static] Return true iff the first character is a lower-case character.
StringUtil
  
isUpperCase(str:String):Boolean
[static] Return true iff the first character is an upper-case character.
StringUtil
  
isWhitespace(character:String):Boolean
[static]
StringUtil
  
pad(str:String, length:int, padChar:String = " "):String
[static] Append 0 or more copies of the padChar String to the input String until it is at least the specified length.
StringUtil
  
parseBoolean(str:String):Boolean
[static] Parse a Boolean from a String, throwing an ArgumentError if the String contains invalid characters.
StringUtil
  
parseInteger(str:String, radix:uint = 0):int
[static] Parse an integer more anally than the built-in parseInt() function, throwing an ArgumentError if there are any invalid characters.
StringUtil
  
parseNumber(str:String):Number
[static] Parse a Number from a String, throwing an ArgumentError if there are any invalid characters.
StringUtil
  
parseUnsignedInteger(str:String, radix:uint = 0):uint
[static] Parse an integer more anally than the built-in parseInt() function, throwing an ArgumentError if there are any invalid characters.
StringUtil
  
parseURLs(s:String):Array
[static] Locate URLs in a string, return an array in which even elements are plain text, odd elements are urls (as Strings).
StringUtil
  
prepad(str:String, length:int, padChar:String = " "):String
[static] Prepend 0 or more copies of the padChar String to the input String until it is at least the specified length.
StringUtil
  
simpleToString(obj:Object, fieldNames:Array = null):String
[static] Return a pretty basic toString of the supplied Object.
StringUtil
  
startsWith(str:String, substr:String):Boolean
[static] Does the specified string start with the specified substring.
StringUtil
  
substitute(str:String, ... args):String
[static] Substitute "{n}" tokens for the corresponding passed-in arguments.
StringUtil
  
toBytes(s:String):ByteArray
[static] Turn the specified String, containing only ascii characters, into a ByteArray.
StringUtil
  
toColorString(c:uint, prefix:String = "0x"):String
[static] Format the specified uint as a String color value, for example "0x000000".
StringUtil
  
toHex(n:uint, width:uint):String
[static] Return a hexadecimal representation of an unsigned int, potentially left-padded with zeroes to arrive at of precisely the requested width, e.g.
StringUtil
  
toString(obj:*, refs:Dictionary = null):String
[static] Nicely format the specified object into a String.
StringUtil
  
trim(str:String):String
[static] Utility function that strips whitespace from the beginning and end of a String.
StringUtil
  
trimBeginning(str:String):String
[static] Utility function that strips whitespace from the beginning of a String.
StringUtil
  
trimEnd(str:String):String
[static] Utility function that strips whitespace from the end of a String.
StringUtil
  
truncate(s:String, maxLength:int, append:String = ""):String
[static] Truncate the specified String if it is longer than maxLength.
StringUtil
  
unhexlate(hex:String):ByteArray
[static] Turn a hexlated String back into a ByteArray.
StringUtil
Protected Methods
 MethodDefined by
  
parseInt0(str:String, radix:uint, allowNegative:Boolean):Number
[static] Internal helper function for parseInteger and parseUnsignedInteger.
StringUtil
  
validateDecimal(str:String, allowDot:Boolean, allowTrailingE:Boolean):String
[static] Internal helper function for parseNumber.
StringUtil
Protected Constants
 ConstantDefined by
  DECIMAL : Array
[static] Decimal digits.
StringUtil
  HEX : Array
[static] Hexidecimal digits.
StringUtil
  URL_REGEXP : RegExp
[static] A regular expression that finds URLs.
StringUtil
Method detail
capitalize()method
public static function capitalize(s:String):String

Returns a version of the supplied string with the first letter capitalized.

Parameters
s:String

Returns
String
compare()method 
public static function compare(s1:String, s2:String):int

Compares two String values, returning -1, 0, or 1. Case-sensitive.

Parameters
s1:String
 
s2:String

Returns
int
compareIgnoreCase()method 
public static function compareIgnoreCase(s1:String, s2:String):int

Compares two String values, returning -1, 0, or 1. Not case-sensitive.

Parameters
s1:String
 
s2:String

Returns
int
deNull()method 
public static function deNull(str:String):String

Return the specified String, or "" if it is null.

Parameters
str:String

Returns
String
endsWith()method 
public static function endsWith(str:String, substr:String):Boolean

Does the specified string end with the specified substring.

Parameters
str:String
 
substr:String

Returns
Boolean
formatNumber()method 
public static function formatNumber(n:Number):String

Format the specified number, nicely, with commas. TODO: format specifyer, locale handling, etc. We'll probably move this into a NumberFormat-style class.

Parameters
n:Number

Returns
String
fromBytes()method 
public static function fromBytes(bytes:ByteArray):String

Turn the specified byte array, containing only ascii characters, into a String.

Parameters
bytes:ByteArray

Returns
String
hashCode()method 
public static function hashCode(str:String):int

Get a hashCode for the specified String. null returns 0. This hashes identically to Java's String.hashCode(). This behavior has been useful in various situations.

Parameters
str:String

Returns
int
hexdump()method 
public static function hexdump(bytes:ByteArray):String

Create line-by-line hexadecimal output with a counter, much like the 'hexdump' Unix utility. For debugging purposes.

Parameters
bytes:ByteArray

Returns
String
hexlate()method 
public static function hexlate(bytes:ByteArray):String

Generates a string from the supplied bytes that is the hex encoded representation of those byts. Returns the empty String for a null or empty byte array.

Parameters
bytes:ByteArray

Returns
String
isBlank()method 
public static function isBlank(str:String):Boolean

Is the specified string null, empty, or does it contain only whitespace?

Parameters
str:String

Returns
Boolean
isLowerCase()method 
public static function isLowerCase(str:String):Boolean

Return true iff the first character is a lower-case character.

Parameters
str:String

Returns
Boolean
isUpperCase()method 
public static function isUpperCase(str:String):Boolean

Return true iff the first character is an upper-case character.

Parameters
str:String

Returns
Boolean
isWhitespace()method 
public static function isWhitespace(character:String):Boolean

Parameters
character:String

Returns
Boolean — true if the specified String is == to a single whitespace character.
pad()method 
public static function pad(str:String, length:int, padChar:String = " "):String

Append 0 or more copies of the padChar String to the input String until it is at least the specified length.

Parameters
str:String
 
length:int
 
padChar:String (default = " ")

Returns
String
parseBoolean()method 
public static function parseBoolean(str:String):Boolean

Parse a Boolean from a String, throwing an ArgumentError if the String contains invalid characters. "1", "0", and any capitalization variation of "true" and "false" are the only valid input values.

Parameters
str:String — the String to parse.

Returns
Boolean
parseInt0()method 
protected static function parseInt0(str:String, radix:uint, allowNegative:Boolean):Number

Internal helper function for parseInteger and parseUnsignedInteger.

Parameters
str:String
 
radix:uint
 
allowNegative:Boolean

Returns
Number
parseInteger()method 
public static function parseInteger(str:String, radix:uint = 0):int

Parse an integer more anally than the built-in parseInt() function, throwing an ArgumentError if there are any invalid characters. The built-in parseInt() will ignore trailing non-integer characters.

Parameters
str:String — The string to parse.
 
radix:uint (default = 0) — The radix to use, from 2 to 16. If not specified the radix will be 10, unless the String begins with "0x" in which case it will be 16, or the String begins with "0" in which case it will be 8.

Returns
int
parseNumber()method 
public static function parseNumber(str:String):Number

Parse a Number from a String, throwing an ArgumentError if there are any invalid characters. 1.5, 2e-3, -Infinity, Infinity, and NaN are all valid Strings.

Parameters
str:String — the String to parse.

Returns
Number
parseUnsignedInteger()method 
public static function parseUnsignedInteger(str:String, radix:uint = 0):uint

Parse an integer more anally than the built-in parseInt() function, throwing an ArgumentError if there are any invalid characters. The built-in parseInt() will ignore trailing non-integer characters.

Parameters
str:String — The string to parse.
 
radix:uint (default = 0) — The radix to use, from 2 to 16. If not specified the radix will be 10, unless the String begins with "0x" in which case it will be 16, or the String begins with "0" in which case it will be 8.

Returns
uint
parseURLs()method 
public static function parseURLs(s:String):Array

Locate URLs in a string, return an array in which even elements are plain text, odd elements are urls (as Strings). Any even element may be an empty string.

Parameters
s:String

Returns
Array
prepad()method 
public static function prepad(str:String, length:int, padChar:String = " "):String

Prepend 0 or more copies of the padChar String to the input String until it is at least the specified length.

Parameters
str:String
 
length:int
 
padChar:String (default = " ")

Returns
String
simpleToString()method 
public static function simpleToString(obj:Object, fieldNames:Array = null):String

Return a pretty basic toString of the supplied Object.

Parameters
obj:Object — the object to be string'd
 
fieldNames:Array (default = null) — the names of fields to print, or null to print all public variables.

Returns
String
startsWith()method 
public static function startsWith(str:String, substr:String):Boolean

Does the specified string start with the specified substring.

Parameters
str:String
 
substr:String

Returns
Boolean
substitute()method 
public static function substitute(str:String, ... args):String

Substitute "{n}" tokens for the corresponding passed-in arguments.

Parameters
str:String
 
... args

Returns
String
toBytes()method 
public static function toBytes(s:String):ByteArray

Turn the specified String, containing only ascii characters, into a ByteArray.

Parameters
s:String

Returns
ByteArray
toColorString()method 
public static function toColorString(c:uint, prefix:String = "0x"):String

Format the specified uint as a String color value, for example "0x000000".

Parameters
c:uint — the uint value to format.
 
prefix:String (default = "0x") — the prefix to place in front of it.

Returns
String
toHex()method 
public static function toHex(n:uint, width:uint):String

Return a hexadecimal representation of an unsigned int, potentially left-padded with zeroes to arrive at of precisely the requested width, e.g. toHex(131, 4) -> "0083"

Parameters
n:uint
 
width:uint

Returns
String
toString()method 
public static function toString(obj:*, refs:Dictionary = null):String

Nicely format the specified object into a String.

Parameters
obj:*
 
refs:Dictionary (default = null)

Returns
String
trim()method 
public static function trim(str:String):String

Utility function that strips whitespace from the beginning and end of a String.

Parameters
str:String

Returns
String
trimBeginning()method 
public static function trimBeginning(str:String):String

Utility function that strips whitespace from the beginning of a String.

Parameters
str:String

Returns
String
trimEnd()method 
public static function trimEnd(str:String):String

Utility function that strips whitespace from the end of a String.

Parameters
str:String

Returns
String
truncate()method 
public static function truncate(s:String, maxLength:int, append:String = ""):String

Truncate the specified String if it is longer than maxLength. The string will be truncated at a position such that it is maxLength chars long after the addition of the 'append' String.

Parameters
s:String — a String to add to the truncated String only after truncation.
 
maxLength:int
 
append:String (default = "")

Returns
String
unhexlate()method 
public static function unhexlate(hex:String):ByteArray

Turn a hexlated String back into a ByteArray.

Parameters
hex:String

Returns
ByteArray
validateDecimal()method 
protected static function validateDecimal(str:String, allowDot:Boolean, allowTrailingE:Boolean):String

Internal helper function for parseNumber.

Parameters
str:String
 
allowDot:Boolean
 
allowTrailingE:Boolean

Returns
String
Constant detail
DECIMALconstant
protected static const DECIMAL:Array

Decimal digits.

HEXconstant 
protected static const HEX:Array

Hexidecimal digits.

URL_REGEXPconstant 
protected static const URL_REGEXP:RegExp

A regular expression that finds URLs.