simplemagik
simplemagik

Reputation: 37

How to split text by lines

Flex 3.

I created a TextArea object. Then I typed some text in it. Then i got this text useing TextArea.text property.

How can I split text I got by lines and convert it to the array of strings?

Upvotes: 0

Views: 5783

Answers (3)

Christian Nunciato
Christian Nunciato

Reputation: 10409

If "\n" doesn't work for you, try "\r" (carriage return). Here's some code demonstrating how it'd work -- just type into the box and you'll see the array contents change:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onCreationComplete(event)">

    <mx:Script>
        <![CDATA[

            import mx.events.CollectionEvent;
            import mx.collections.ArrayCollection;
            import mx.events.FlexEvent;

            [Bindable]
            private var ac:ArrayCollection;

            protected function onCreationComplete(event:FlexEvent):void
            {
                ac = new ArrayCollection();
                ac.addEventListener(CollectionEvent.COLLECTION_CHANGE, onCollectionChange);     
            }

            protected function onKeyUp(event:KeyboardEvent):void
            {
                ac.source = event.target.text.split("\r");
            }

            protected function onCollectionChange(event:CollectionEvent):void
            {
                contents.text = ac.source.join("\r");           
            }

        ]]>
    </mx:Script>

    <mx:TextArea id="input" x="19" y="22" width="273" height="175" keyUp="onKeyUp(event)" />
    <mx:TextArea id="contents" x="112" y="249" width="180" height="175" editable="false" />
    <mx:Label x="19" y="222" text="Array Length:" fontWeight="bold" />
    <mx:Label x="37" y="250" text="Contents:" fontWeight="bold" />
    <mx:Label x="111" y="222" text="{ac.length}" />

</mx:Application>

Hope it helps!

Upvotes: 1

Aaron
Aaron

Reputation: 7541

According to this site, the character for a line feed feed is "\n". Using the Split() function with "\n" you should be able to split the string into an array of string separated by line feeds.

Upvotes: 0

Charlie Martin
Charlie Martin

Reputation: 112366

What does "split by lines" mean to you? The usual thing is to scan the text for the last blank before textwidth, and call that a line.

Upvotes: 0

Related Questions