如何按行拆分文本

时间:2021-04-19 15:00:33

Flex 3.

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

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

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

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

3 个解决方案

#1


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:

如果“\ 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>

Hope it helps!

希望能帮助到你!

#2


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.

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

#3


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.

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

#1


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:

如果“\ 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>

Hope it helps!

希望能帮助到你!

#2


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.

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

#3


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.

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