0

弹性 3。

我创建了一个 TextArea 对象。然后我在里面输入了一些文字。然后我使用 TextArea.text 属性得到了这个文本。

如何拆分按行获得的文本并将其转换为字符串数组?

4

3 回答 3

1

如果“\n”对您不起作用,请尝试“\r”(回车)。这里有一些代码演示了它是如何工作的——只需在框中输入,你就会看到数组内容发生了变化:

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

希望能帮助到你!

于 2009-06-03T23:13:03.433 回答
0

“按行分割”对您意味着什么?通常的做法是扫描文本以查找 textwidth 之前的最后一个空白,并将其称为一行。

于 2009-06-03T21:30:40.270 回答
0

根据这个站点,换行符的字符是“\n”。使用带有 "\n" 的 Split() 函数,您应该能够将字符串拆分为由换行符分隔的字符串数组。

于 2009-06-03T21:31:53.810 回答