/* CASA Framework for ActionScript 3.0 Copyright (c) 2008, Contributors of CASA Framework All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of the CASA Framework nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.casaframework.util { /** @author Aaron Clinger @author Mike Creighton @author David Nelson @version 02/26/08 */ public class StringUtil { /** Transforms source String to per word capitalization. @param source: String to return as title cased. @return String with capitalized words. */ public static function toTitleCase(source:String):String { var pattern:RegExp = /\b([a-z])/g; return source.replace(pattern, function($1:String):String{return $1.toUpperCase()}); } /** Removes all numeric characters from String. @param source: String to remove numbers from. @return String with numbers removed. */ public static function removeNumbersFromString(source:String):String { var pattern:RegExp = /[0-9]/g; return source.replace(pattern, ''); } /** Removes all non numeric characters from String. @param source: String to return numbers from. @return String containing only numbers. */ public static function getNumbersFromString(source:String):String { var pattern:RegExp = /[^0-9]/g; return source.replace(pattern, ''); } /** Determines if String contains search String. @param source: String to search in. @param search: String to search for. @return Returns the frequency of the search term found in source String. */ public static function contains(source:String, search:String):uint { var pattern:RegExp = new RegExp(search, 'g'); return source.match(pattern).length; } /** Strips whitespace (or other characters) from the beginning of a String. @param source: String to remove characters from. @param removeChars: Characters to strip (case sensitive). @return String with characters removed. */ public static function trimLeft(source:String, removeChars:String = " \n\t\r"):String { var pattern:RegExp = new RegExp('^[' + removeChars + ']+',''); return source.replace(pattern, ''); } /** Strips whitespace (or other characters) from the end of a String. @param source: String to remove characters from. @param removeChars: Characters to strip (case sensitive). @return String with characters removed. */ public static function trimRight(source:String, removeChars:String = " \n\t\r"):String { var pattern:RegExp = new RegExp('[' + removeChars + ']+$',''); return source.replace(pattern, ''); } /** Strips whitespace (or other characters) from the beginning and end of a String. @param source: String to remove characters from. @param removeChars: Characters to strip (case sensitive). @return String with characters removed. */ public static function trim(source:String, removeChars:String = " \n\t\r"):String { var pattern:RegExp = new RegExp('^[' + removeChars + ']+|[' + removeChars + ']+$', 'g'); return source.replace(pattern, ''); } /** Removes additional spaces from String. @param source: String to remove extra spaces from. @return String with additional spaces removed. */ public static function removeExtraSpaces(source:String):String { var pattern:RegExp = /( )+/g; return source.replace(pattern, ' '); } /** Removes tabs, linefeeds, carriage returns and spaces from String. @param source: String to remove whitespace from. @return String with whitespace removed. */ public static function removeWhitespace(source:String):String { var pattern:RegExp = new RegExp('[ \n\t\r]', 'g'); return source.replace(pattern, ''); } /** Removes characters from a source String. @param source: String to remove characters from. @param remove: String describing characters to remove. @return String with characters removed. */ public static function remove(source:String, remove:String):String { return StringUtil.replace(source, remove, ''); } /** Replaces target characters with new characters. @param source: String to replace characters from. @param remove: String describing characters to remove. @param replace: String to replace removed characters. @return String with characters replaced. */ public static function replace(source:String, remove:String, replace:String):String { var pattern:RegExp = new RegExp(remove, 'g'); return source.replace(pattern, replace); } /** Removes a character at a specific index. @param source: String to remove character from. @param position: Position of character to remove. @return String with character removed. */ public static function removeAt(source:String, position:int):String { return StringUtil.replaceAt(source, position, ''); } /** Replaces a character at a specific index with new characters. @param source: String to replace characters from. @param position: Position of character to replace. @param replace: String to replace removed character. @return String with character replaced. */ public static function replaceAt(source:String, position:int, replace:String):String { var parts:Array = source.split(''); parts.splice(position, 1, replace); return parts.join(''); } /** Adds characters at a specific index. @param source: String to add characters to. @param position: Position in which to add characters. @param addition: String to add. @return String with characters added. */ public static function addAt(source:String, position:int, addition:String):String { var parts:Array = source.split(''); parts.splice(position, 0, addition); return parts.join(''); } /** Extracts all the unique characters from a source String. @param source: String to find unique characters within. @return String containing unique characters from source String. */ public static function getUniqueCharacters(source:String):String { var unique:String = ''; var i:uint = 0; var char:String; while (i < source.length) { char = source.charAt(i); if (unique.indexOf(char) == -1) unique += char; i++; } return unique; } } }