Flash / Flex / ActionScript/String/split — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 09:19, 26 мая 2010
Содержание
- 1 Converting a String into an Array
- 2 Converting Strings to Arrays
- 3 If you use another delimiter in the string, you need to specify it as the argument for the
- 4 Parsing a String into Words
- 5 split() method can be called using a RegExp object instead of a string as the delimiter
- 6 split method returns an array
- 7 Use regular expression in split method
- 8 Use split method to get the file name and its extension name
- 9 Use split(String, index) to split String to array of strings
- 10 Use split(String) method to split String to array of strings
- 11 Use split to convert string to string array
Converting a String into an Array
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var sValue:String = "a,b,c,d,e,f";
var aValues:Array = sValue.split(",");
for (var i = 0; i < aValues.length; i++){
trace(aValues[i]);
}
}
}
}
a
b
c
d
e
f
Converting Strings to Arrays
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var keywords:String = "people, new york, friends, picnic";
var tags:Array = keywords.split(",");
trace(tags);
}
}
}
If you use another delimiter in the string, you need to specify it as the argument for the
split() method appropriately.
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var sValue:String = "a b c d e f";
var aValues = sValue.split(" ");
for (var i = 0; i < aValues.length; i++){
trace(aValues[i]);
}
}
}
}
a
b
c
d
e
f
Parsing a String into Words
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var example:String = "This is a string of words";
var words:Array = example.split(" ");
for ( var i:int = 0; i < words.length; i++ ) {
trace( words[i] );
}
}
}
}
split() method can be called using a RegExp object instead of a string as the delimiter
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var reCase:RegExp = new RegExp("[ ,]", "ig");
var sVal = new String("ActionScript Bible, Flash Bible");
var aVals:Array = sVal.split(reCase);
trace(aVals);
}
}
}
split method returns an array
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var example:String = "a string";
var characters:Array = example.split( "" );
for ( var i:int = 0; i < characters.length; i++ ) {
trace( characters[i] );
}
}
}
}
Use regular expression in split method
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var example:String = "Here are some words. Also, here is some punctuation!";
var words:Array = example.split( /[^a-zA-Z0-9]+/ );
for (var i:int = 0; i < words.length; i++ ) {
trace( words[i] );
}
}
}
}
Use split method to get the file name and its extension name
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var filename:String = "document.jpg";
var nameParts:Array = filename.split(".");
var extensionless:String = nameParts[0];
trace ("The filename is " + extensionless);
var extension:String = nameParts[1];
trace ("The file extension is " + extension);
}
}
}
Use split(String, index) to split String to array of strings
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var sentence:String = "The quick brown fox jumped over the lazy dog";
var words:Array = sentence.split("", 4); // limit to 4 elements.
trace(words);
}
}
}
Use split(String) method to split String to array of strings
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var state:String = "Mississippi";
var foo:Array = state.split("ss");
trace(foo);
}
}
}
Use split to convert string to string array
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var example:String = "Here are some words. Also, here is some punctuation!";
var words:Array = example.split( " " );
for ( var i:int = 0; i < words.length; i++ ) {
trace( words[i] );
}
}
}
}