Flash / Flex / ActionScript/String/substr

Материал из Web эксперт
Версия от 08:14, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Optionally omit the second parameter, substr returns a substring starting at the specified index and going to the end of the original string.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var sTitle:String = new String("ActionScript Bible");
        trace(sTitle.substr(6));  // Displays: Script Bible
    }
  }
}



Specifying a negative value simply counts backward from the end of the string where -1 is the last character.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var sTitle:String = new String("ActionScript AAAAA");
        trace(sTitle.substr(-12, 6));  // Displays: Script
    }
  }
}



substring method returns a substring starting with the starting index specified and containing all the characters up to, but not including, the ending index.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var sTitle:String = new String("ActionScript");
        trace(sTitle.substring(6, 12));  // Displays: Script
    }
  }
}



Use lastIndexOf to get the file name without extension name

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var filename:String = "document.jpg";
        var extensionIndex:Number = filename.lastIndexOf( "." );
        
        var extensionless:String = filename.substr( 0, extensionIndex ); 
        trace( "The filename is " + extensionless );

    }
  }
}



Working with Substring Values: substr

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var sTitle:String = new String("ActionScript Bible");
        trace(sTitle.substr(6, 6));  // Displays: Script
        trace(sTitle.substr(0, 6));  // Displays: Action
        trace(sTitle.substr(0, 12)); // Displays: ActionScript
    }
  }
}