Question

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
Was it helpful?

Solution

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?)

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top