views:

1290

answers:

3

When I create a TextField in AS3 with multiline set to true and equate the text to say:

"Hola \r hola"

I am unable to retrieve the index position of \r using indexOf function, it always returns -1

Does anyone know what I'm doing wrong?

var txt:TextField;
txt.multiline = true;

txt.text = "Hola \r hola";

//txt now renders fine with the line break

trace(txt.indexOf("\r")); //Returns -1, should return the valid index of \r in txt
+1  A: 

Looks to me that you're trying to get the index of the TextField instead of the TextField.text that you're interested in.

trace(txt.text.indexOf("\r"));

could work a bit better.

Mikko Tapionlinna
Thanks for getting back, I also tried that, but it doesnt work still...
Brian, does'nt txt.text return a string? Sure, I could have written my text to be a bit more clear, but my trace line is correct.
Mikko Tapionlinna
It does but it you use toString(). you get the auto-completion popup which is very useful for beginners, he would have figured this out long ago with the help of the completion window in my opinion.
Brian Hodge
I will, however, retract that you are incorrect.
Brian Hodge
+2  A: 

Following Mikko's answer I gave it a try :

var textField:TextField = addChild(new TextField()) as TextField;
textField.multiline = true;

textField.text = "test \r test";

trace("result>" + textField.text.indexOf("\r"));

This code traces :

result>5

... Just as expected.

If it still doesn't work for you, first try to search for another character than \r, if this works also try to search for \n. Maybe the line feed gets transformed somehow. (which OS are you on?)

Theo.T
Thank you for writing my quick answer better, I failed to see the initation problem. :)
Mikko Tapionlinna
Thanks, its one of those days when u make a silly error and nothing seems to work. Bad me. Works now.
+1  A: 

Ok so first things first,

You have not instantiated your textfield, you merely made a reference.

Secondly, indexOf, is not available to the TextField class, but to the String class, so use the following and you will have no more issues. It traces out fine for me.

var txt:TextField = new TextField();
addChild(txt);
txt.multiline = true;
txt.text = "Hola \r hola";

trace(txt.text.toString().indexOf("\r"));

Theo essentially has it correct, but I thought I would try an make it a little more clear.

Hope that I could help. Also I would suggest checking into regular expressions, which have easy ways to find white-space characters or any other pattern you can think of.

Brian Hodge blog.hodgedev.com hodgedev.com

Brian Hodge
Brian, indexOf is not available to the TextField class, but txt.text returns an String, so you do not need to convert it to String again. Check here: http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/text/TextField.html#text
Mikko Tapionlinna
I know that it is a string, but by saying toString() once you type the period you get the auto-completion box popup to show what properties and methods are available :) Thought it would help tjames.
Brian Hodge