在ActionScript中替换子字符串

问题
      我们需要一个替换所有字符串的方法。

      解决方法
      可以使用正则表达式或者使用split和jion方法。

      详细解释
      我们可以使用下面两种方法:
  •         private function testFlexStringReplaceAll():void {
  • var strSource:String = "Li_guo_Liang.com";
  • trace(strSource + " - " + replaceAllBySplit(strSource, "_", ""));
  • trace(strSource + " - " + replaceAllByRegex(strSource, "_", ""));
  • }

  • /**
  • * Repalce all by split and join;
  • */
  • public static function replaceAllBySplit(strSource:String, strReplaceFrom:String, strRepalceTo:String):String {
  • return strSource == null ? null : strSource.split(strReplaceFrom).join(strRepalceTo);
  • }

  • /**
  • * Replace all by RegEx;
  • */
  • public static function replaceAllByRegex(strSource:String, strReplaceFrom:String, strRepalceTo:String):String {
  • return strSource == null ? null : strSource.replace(new RegExp(strReplaceFrom, 'g'), strRepalceTo);
  • }

你可能感兴趣的:(正则表达式,String,function,null,regex,actionscript)