How to determine the index in a string for a line-break in Actionscript?
August 22, 2004 3:05 AM   Subscribe

Actionscript: Is there any way to determine the index in a string of a line-break, when that string is presented in a text field?
posted by FidelDonson to Computers & Internet (4 answers total)
 
Try this:


//txtInput is an input text field

btnSubmit.onRelease = function()
{
var strInput:String = txtInput.text;
strInput = escape(strInput);
trace(strInput);
trace(strInput.indexOf("%0"));
}
posted by grumblebee at 5:32 AM on August 22, 2004


that last line before the close-curly brace should be:

trace(strInput.indexOf("%0D"));

%0D is the escape code for carriage return.


========

This will also work:

btnSubmit.onRelease = function()
{
var strInput:String = txtInput.text;
for (var i:Number = 0; i < strinput.length; i++)br> {
if(strInput.charCodeAt(i) == 13) //ASCII code 13 = carriage return
{
trace(i);
break;
}
}
}


...and it avoids the hack of converting the string into escape codes. The odd thing is that this SHOULD work:

var strInput:String = txtInput.text;
var strReturn:String = String.fromCharAt(13); //carriage return
trace (strInput.indexOf(strReturn));


...but it doesn't work. I'm not sure why it doesn't detect ASCII code 13 in the string when I test this way, but it DOES detect it when I use one of the above methods. Anyone know???
posted by grumblebee at 7:07 AM on August 22, 2004


Okay, I don't know what my problem was, but this DOES work:

btnSubmit.onRelease = function()
{
var strInput:String = txtInput.text;
var strReturn:String = String.fromCharCode(13);
trace(strInput.indexOf(strReturn));
}
posted by grumblebee at 8:49 AM on August 22, 2004


Response by poster: Works like a charm - thank you very much.
posted by FidelDonson at 11:34 AM on August 23, 2004


« Older Male Patsy Cline Movie   |   Presidential biographies Newer »
This thread is closed to new comments.