1

I embed a text file into my Flex project and read its contents using code like this:

[Embed(source = "../../data/abc.txt", mimeType = "application/octet-stream")]
private var r_Abc:Class;

...

var xx:ByteArray = new r_Abc();
var abc:String = xx.toString();

The contents of the file is abc. The problem is that the string loaded from the file is not comparable to other strings even though when printed or viewed in the debugger (in FlashDevelop) it seems to be perfectly fine.

trace(abc);  // abc
trace("abc" == abc);   // false

How do I convert it into a proper string? I tried to use the string methods such as substring to create a copy, but that does not seem to be the solution.

1
  • Really strange! I ran the sample code. My abc-variable is undefined. Commented Sep 28, 2011 at 9:55

1 Answer 1

1

Here's my sample:

<?xml version="1.0" encoding="utf-8"?>
<s:Application minWidth="955" minHeight="600"
               creationComplete="creationCompleteHandler(event)"
               xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx">

    <fx:Script>
        <![CDATA[
            import mx.core.ByteArrayAsset;
            import mx.events.FlexEvent;

            // my file is "ABC "
            // strangely enough if I remove the space at the end the string in code is empty
            [Embed(source="data/abc.txt", mimeType="application/octet-stream")]
            private var abcFile:Class;

            protected function creationCompleteHandler(event:FlexEvent):void
            {
                var abcByteArray:ByteArrayAsset = ByteArrayAsset(new abcFile());
                var abc:String = abcByteArray.readUTFBytes(abcByteArray.length);

                trace(abc); // ABC (has a space at the end)
                trace(abc == "ABC "); // true, but notice the space at the end
            }
        ]]>
    </fx:Script>
</s:Application>

My suggestion is to check for trailing spaces, new lines. Also try to place some sort of an EOF character at the end of the file.

Sign up to request clarification or add additional context in comments.

1 Comment

I ended up using your solution. I had to add a newline to my files too. Pretty weird :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.