【问题标题】:How to split text by lines如何按行分割文本
【发布时间】:2010-10-31 03:31:18
【问题描述】:

弹性 3。

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

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

【问题讨论】:

    标签: apache-flex flash actionscript mxml


    【解决方案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>
    

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我首先想到的是用这种方式解决这个问题。但它不起作用!
      【解决方案3】:

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

      【讨论】:

      • 我需要将多行文本转换为字符串数组,其中每个元素都是单独的行(文本字符串?)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 2021-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-12
      • 2015-07-06
      相关资源
      最近更新 更多