Ultrashock Forums > Flash > AIR
get text range between ceratin number of text lines

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
get text range between ceratin number of text lines
Old 2009-07-02

Hi to all,

I'm working on an AIR application using Flex Builder 3 with Flex SDK 4.0 installed. The main idea of the application is to read an external text file (*.txt) into a TextArea and after clicking a button I want some text range (e.g. from line 100-200) to be copied into another TextArea.

I've made almost everything, but I don't know how to extract a specific range of text between certain text lines.

I alredy made a code to get the exact number of lines of the whole input text file:

PHP Code:
//get number of lines
function getNumLines(event:MouseEvent):void{
    
saveButton.enabled true;
    var 
nLines:uint inputField.mx_internal::getTextField().numLines;
    for (var 
i:Number 0;i<nLines;i++){
          var 
Line1:String inputField.mx_internal::getTextField().getLineText(i);
          var 
str:String = (1+i) + " " Line1;
           
outputField.text += str;
    }
}
extractButton.addEventListener(MouseEvent.CLICK,getNumLines); 
I've searched over the net about this problem and I've found getText() method which could be possible solution to fix this problem, but I really don't know how to implement it in my case.

If someone have an idea or solution, will be of great help!

Thanks in advance,
Nikola
postbit arrow 3 comments | 676 views postbit arrow Reply: with Quote   
Registered User
minopharma is offline
seperator
Posts: 20
2005-09-04
Age: 30
seperator

Ultrashock Member Comments:
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2009-07-02 #2 Old  
Why are you using the mx_internal namespace? You can get the number of lines in a TextField using the TextField.numLines property:

Code:
var numLines:int = myTextField.numLines;
If you want to get a range of lines from a TextField you could use the following function:

Code:
function getLines( textField:TextField, start:int, count:int ):String
{
	var a:Array = [];
	var i:int = start;
	var n:int = start + count;
	
	while( i < n ) {
		a.push( textField.getLineText( i ) );
		i ++;
	}

	var s:String = a.join( "" );
	
	if( s.substr( -1 ) == "\r" ) {
		s = s.substr( 0, -1 );
	}

	return s;
}
The start parameter is zero-based, if you wanted to get the first 10 lines of text you would do this:

Code:
var range:String = getLines( myTextField, 0, 10 );
Hope that helps.
Reply With Quote  
minopharma minopharma is offline 2009-07-03 #3 Old  
Thank you Nutrox for your effort and the code. I've tested this code and it works great.

But I've faced with another problem. In fact, I have different text files with different number of lines (in my case: 25509 lines for the first text file for example). Each of the text files contain the string "Standard orientation:" which is repeated several times through the text.

I need the number of the line where is the last occurrence of this specific string and this number I'll put in your function getLines() as start variable.

How to do that?

Thanks,
Nikola
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2009-07-03 #4 Old  
I think this should work:

Code:
// get the character index
var charIndex:int = myTextField.text.lastIndexOf( "Standard orientation" );

// make sure the character index is valid
if( charIndex > -1 )
{
    // get the line index
    var lineIndex:int = myTextField.getLineIndexOfChar( charIndex );

    // get the lines
    var lines:String = getLines( myTextField, lineIndex, lineCount );
}
I'm not sure how fast this is going to be with lines 25509 of text though, that is an extremely large amount of text to be working with in Flash.
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: