Monday, November 11, 2013

Extracting Specific text part from a String [Actionscript 3]

In actionscript 3, this can be done using the methods substr and substring.
substr(starting index,length);
substring(starting index,end index);

note :- all the parameters are numbers.
For substr
  • starting index : starting point of extraction (in other words after how many letters)
  • length : length of the text that should extracted from the starting point
eg :- Let’s consider this text “Extracting”
starting index = 2
length = 5
Then the text getting extracted is “tract”
For substring
  • starting index : starting point of extraction
  • end index : end point of extraction
eg :- Let’s consider this text “Extracting”
starting index = 2
end index = 5
Then the text getting extracted is “tra”
Sample as3 code :
 
<Code>
 
// creating a new String called myText and print it
var myText:String = new String("Extracting");
trace(myText); //output: Extracting


//<substr> extracting part of myText to a new variable called extractedText1 and print it
var extractedText1 = myText.substr(2,5);
trace(extractedText1); //output: tract


//<substring> extracting part of myText to a new variable called extractedText1 and print
var extractedText2 = myText.substring(2,5);
trace(extractedText2); //output: tra

No comments:

Post a Comment