Class org.as2lib.util.StringUtil

org.as2lib.core.BasicClass
   +--org.as2lib.util.StringUtil

Description

StringUtil offers a lot of different methods to work with strings.

Method Index

addSpaceIndent(), assureLength(), checkEmail(), contains(), endsWith(), firstChar(), lastChar(), leftTrim(), leftTrimForChar(), leftTrimForChars(), multiply(), replace(), rightTrim(), rightTrimForChar(), rightTrimForChars(), startsWith(), trim(), ucFirst(), ucWords()

Inherited from BasicClass

toString()

Method Detail

replace

static public function replace(string:String, what:String, to:String):String

Replaces all occurencies of the passed-in string what with the passed-in string to in the passed-in string string.

Parameters

stringthe string to replace the content of
whatthe string to search and replace in the passed-in string
tothe string to insert instead of the passed-in string what

Return

the result in which all occurences of the what string are replaced by the to string

trim

static public function trim(string:String):String

Removes all empty characters at the beginning and at the end of the passed-in string.

Characters that are removed: spaces " ", line forwards "\n" and extended line forwarding "\t\n".

Parameters

stringthe string to trim

Return

the trimmed string

leftTrim

static public function leftTrim(string:String):String

Removes all empty characters at the beginning of a string.

Characters that are removed: spaces " ", line forwards "\n" and extended line forwarding "\t\n".

Parameters

stringthe string to trim

Return

the trimmed string

rightTrim

static public function rightTrim(string:String):String

Removes all empty characters at the end of a string.

Characters that are removed: spaces " ", line forwards "\n" and extended line forwarding "\t\n".

Parameters

stringthe string to trim

Return

the trimmed string

leftTrimForChars

static public function leftTrimForChars(string:String, chars:String):String

Removes all characters at the beginning of the string that match to the set of chars.

This method splits all chars and removes occurencies at the beginning.

Example: trace(StringUtil.rightTrimForChars("ymoynkeym", "ym")); // oynkeym trace(StringUtil.rightTrimForChars("monkey", "mo")); // nkey trace(StringUtil.rightTrimForChars("monkey", "om")); // nkey

Parameters

stringthe string to trim
charsthe characters to remove from the beginning of the string

Return

the trimmed string

rightTrimForChars

static public function rightTrimForChars(string:String, chars:String):String

Removes all characters at the end of the string that match to the set of chars.

This method splits all chars and removes occurencies at the end.

Example: trace(StringUtil.rightTrimForChars("ymoynkeym", "ym")); // ymoynke trace(StringUtil.rightTrimForChars("monkey***", "*y")); // monke trace(StringUtil.rightTrimForChars("monke*y**", "*y")); // monke

Parameters

stringthe string to trim
charsthe characters to remove from the end of the string

Return

the trimmed string

leftTrimForChar

static public function leftTrimForChar(string:String, char:String):String

Removes all characters at the beginning of the string that matches the char.

Example: trace(StringUtil.leftTrimForChar("yyyymonkeyyyy", "y"); // monkeyyyy

Parameters

stringthe string to trim
charthe character to remove

Return

the trimmed string

Throws

IllegalArgumentExceptionif you try to remove more than one character

rightTrimForChar

static public function rightTrimForChar(string:String, char:String):String

Removes all characters at the end of the string that matches the passed-in char.

Example: trace(StringUtil.rightTrimForChar("yyyymonkeyyyy", "y"); // yyyymonke

Parameters

stringthe string to trim
charthe character to remove

Return

the trimmed string

Throws

IllegalArgumentExceptionif you try to remove more than one character

checkEmail

static public function checkEmail(email:String):Boolean

Validates the passed-in email adress to a predefined email pattern.

Parameters

emailthe email to check whether it is well-formatted

Return

true if the email matches the email pattern else false

assureLength

static public function assureLength(string:String, length:Number):Boolean

Assures that the passed-in string is bigger or equal to the passed-in length.

Parameters

stringthe string to validate
lengththe length the string should have

Return

true if the length of string is bigger or equal to the expected length else false

Throws

IllegalArgumentExceptionif the expected length is less than 0

contains

static public function contains(string:String, chars:String):Boolean

Evaluates if the passed-in chars are contained in the passed-in string.

This methods splits the chars and checks if any character is contained in the string.

Example: trace(StringUtil.contains("monkey", "kzj0")); // true trace(StringUtil.contains("monkey", "yek")); // true trace(StringUtil.contains("monkey", "a")); // false

Parameters

stringthe string to check whether it contains any of the characters
charsthe characters to look whether any of them is contained in the string

Return

true if one of the chars is contained in the string

startsWith

static public function startsWith(string:String, searchString:String):Boolean

Evaluates if the passed-in stirng starts with the searchString.

Parameters

stringthe string to check
searchStringthe search string that may be at the beginning of string

Return

true if string starts with searchString else false.

endsWith

static public function endsWith(string:String, searchString:String):Boolean

Tests whether the string ends with searchString.

Parameters

stringthe string to check
searchStringthe string that may be at the end of string

Return

true if string ends with searchString

addSpaceIndent

static public function addSpaceIndent(string:String, size:Number):String

Adds a space indent to the passed-in string.

This method is useful for different kind of ASCII output writing. It generates a dynamic size of space indents in front of every line inside a string. ################################################################################

Example: var bigText = "My name is pretty important\n" + "because i am a interesting\n" + "small example for this\n" + "documentation."; var result = StringUtil.addSpaceIndent(bigText, 3);

Contents of result:

   My name is pretty important
      because i am a interesting
      small example for this
      documentation.
 

indent will be floored.

Parameters

stringthe string that contains lines to indent

Return

the indented string

Throws

IllegalArgumentExceptionif the size is smaller than 0

multiply

static public function multiply(string:String, factor:Number):String

Multiplies the passed-in string by the passed-in factor to create long string blocks.

Example: trace("Result: "+StringUtil.multiply(">", 6); // Result: >>>>>>

Parameters

stringthe source string to multiply
factorthe number of times to multiply the string

ucFirst

static public function ucFirst(string:String):String

Capitalizes the first character of the passed-in string.

Parameters

stringthe string of which the first character shall be capitalized

Return

the passed-in string with the first character capitalized

ucWords

static public function ucWords(string:String):String

Capitalizes the first character of every word in the passed-in string.

Parameters

stringthe string of which the first character of every word shall be capitalized

Return

the string with the first character of every word capitalized

firstChar

static public function firstChar(string:String):String

Returns the first character of the passed-in string.

Parameters

stringthe string to return the first character of

Return

the first character of the string

lastChar

static public function lastChar(string:String):String

Returns the last character of the passed-in string.

Parameters

stringthe string to return the last character of

Return

the last character of the string