Posted
Filed under Action Script
[원문] http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/String.html
 actionscript 3.0작업중 trim 관련 함수를 찾아 보다 . 라이브러리에서 trim, replace  함수 구현 되어 있는
것을 발견 하게 되었음 .

package {
    import flash.display.Sprite;

    public class StringExample extends Sprite {
        public function StringExample() {
            var companyStr:String = new String("     Company X");
            var productStr:String = "Product Z Basic     ";
            var emptyStr:String = " ";
            var strHelper:StringHelper = new StringHelper();

            var companyProductStr:String = companyStr + emptyStr + productStr;
            trace("'" + companyProductStr + "'");    // '     Company X Product Z Basic     '

            companyProductStr = strHelper.replace(companyProductStr, "Basic", "Professional");
            trace("'" + companyProductStr + "'");    // '     Company X Product Z Professional     '

            companyProductStr = strHelper.trim(companyProductStr, emptyStr);
            trace("'" + companyProductStr + "'");    // 'Company X Product Z Professional'
        }
    }
}

class StringHelper {
    public function StringHelper() {
    }

    public function replace(str:String, oldSubStr:String, newSubStr:String):String {
        return str.split(oldSubStr).join(newSubStr);
    }

    public function trim(str:String, char:String):String {
        return trimBack(trimFront(str, char), char);
    }

    public function trimFront(str:String, char:String):String {
        char = stringToCharacter(char);
        if (str.charAt(0) == char) {
            str = trimFront(str.substring(1), char);
        }
        return str;
    }

    public function trimBack(str:String, char:String):String {
        char = stringToCharacter(char);
        if (str.charAt(str.length - 1) == char) {
            str = trimBack(str.substring(0, str.length - 1), char);
        }
        return str;
    }

    public function stringToCharacter(str:String):String {
        if (str.length == 1) {
            return str;
        }
        return str.slice(0, 1);
    }
}

StringHelper 클래스의 함수들이 static 타입이 아님으로 이렇게 사용 하면 오류가 발생 한다.
static타입으로 선언후 사용 하자 .



class StringHelper {
 
  public static function replace(str:String, oldSubStr:String, newSubStr:String):String {
   return str.split(oldSubStr).join(newSubStr);
  }
 
  public static function trim(str:String, char:String):String {
   return trimBack(trimFront(str, char), char);
  }
 
  public static function trimFront(str:String, char:String):String {
   char = stringToCharacter(char);
   if (str.charAt(0) == char) {
    str = trimFront(str.substring(1), char);
   }
   return str;
  }
 
  public static function trimBack(str:String, char:String):String {
   char = stringToCharacter(char);
   if (str.charAt(str.length - 1) == char) {
    str = trimBack(str.substring(0, str.length - 1), char);
   }
   return str;
  }
 
  public static function stringToCharacter(str:String):String {
   if (str.length == 1) {
    return str;
   }
   return str.slice(0, 1);
  }
 }

2010/04/15 13:08 2010/04/15 13:08
cash for cars sydney

This is a really interesting post. more cash for cars Sydney, Ezy Cash for Cars is the best place where you can get the best Service of car removal Brisbane the and scrapping your car for cash. Call us cash for cars Brisbane, today at 0499 123 100