Flash / Flex / ActionScript/String/substr
Содержание
- 1 Optionally omit the second parameter, substr returns a substring starting at the specified index and going to the end of the original string.
- 2 Specifying a negative value simply counts backward from the end of the string where -1 is the last character.
- 3 substring method returns a substring starting with the starting index specified and containing all the characters up to, but not including, the ending index.
- 4 Use lastIndexOf to get the file name without extension name
- 5 Working with Substring Values: substr
Optionally omit the second parameter, substr returns a substring starting at the specified index and going to the end of the original string.
<source lang="java">
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 } }
}
</source>
Specifying a negative value simply counts backward from the end of the string where -1 is the last character.
<source lang="java">
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 } }
}
</source>
substring method returns a substring starting with the starting index specified and containing all the characters up to, but not including, the ending index.
<source lang="java">
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 } }
}
</source>
Use lastIndexOf to get the file name without extension name
<source lang="java">
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 );
} }
}
</source>
Working with Substring Values: substr
<source lang="java">
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 } }
}
</source>