• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Manipulating a long text with AS3

Contributor ,
Jul 24, 2018 Jul 24, 2018

Copy link to clipboard

Copied

Hi,

I'm loading a long text from my server to my AIR app.

The text is a weather forecast.

It's presenting like that :

"Today forecast is this one. This morning : the sun will be generous everywhere. This afternoon : clouds will hide the sun. This night : rain will fall. Tomorrow morning : blblablblabla"

I'd like to create multiple strings of each "section". A morning string section, an afternoon string section, a night string section..etc

If we take my morning string for example : how can I delete everything before "This morning" and everything from "this afternoon" (in order to only have, in my morning string : "This morning : the sun will be generous everywhere.")  

So, if I'm correct, I've managed to select from "This morning" by doing that :

var str: String = "Today forecast is this one. This morning : the sun will be generous everywhere. This afternoon : clouds will hide the sun. This night : rain will fall. Tomorrow morning : blblablblabla";

var search_morning_starts: Number = str.indexOf("This");

var morning_str:String = str.substring(search_morning_starts,str.length);

trace(morning_str);

But how can I add to delete everything from "this afternoon" ? (can't say "delete everything after "everywhere" because the word "everywhere" won't be written every time. It depends on the weather of the morning. So I need to delete everything when "this afternoon"appears)

Thanks

TOPICS
ActionScript

Views

575

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 25, 2018 Jul 25, 2018

Then what you can do would be similar but extended to process a bit more.  After you break the string into an array, you can then examine each element to see if it contains a colon ( : ) .  If it does not then you append it to the element before it.... build it into another array if it is easier for you.   I will leave you to getting it to work - it will.  If you happened to leave out more detail that would complicate it beyond that solution then think it thru.

Hint....  use the indexOf() method

...

Votes

Translate

Translate
LEGEND ,
Jul 24, 2018 Jul 24, 2018

Copy link to clipboard

Copied

If that is the standard pattern of the text then you could simply 'split' the string on the periods, and then ignore the first portion.

var forecasts:Array;

forecasts = str.split(".");

That code will create an array that has the following 5 elements in it....

forecasts[0]: Today forecast is this one

forecasts[1]: This morning : the sun will be generous everywhere

forecasts[2]: This afternoon : clouds will hide the sun

forecasts[3]: This night : rain will fall

forecasts[4]: Tomorrow morning : blblablblabla

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 24, 2018 Jul 24, 2018

Copy link to clipboard

Copied

Thank you for your answer but I forgot to precise something (I'll edit my post).

There are more than one sentences per forecast (I just put 1 sentence as an example in my text) so I can't split it with "."

I can have multiple sentences in one forecast like that :

Today forecast is this one. This morning : the sun will be generous everywhere. Wind will be strong but nothing dangerous. Wind speed will be 30 knt. This afternoon : clouds will hide the sun. It may rain during the afternoon but not too much. This night : rain will fall. It will be a full moon. Tomorrow morning : blblablblabla

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 08, 2018 Aug 08, 2018

Copy link to clipboard

Copied

LATEST

var str:String = "Today forecast is this one. This morning : the sun will be generous everywhere. Wind will be strong but nothing dangerous. Wind speed will be 30 knt. This afternoon : clouds will hide the sun. It may rain during the afternoon but not too much. This night : rain will fall. It will be a full moon. Tomorrow morning : blblablblabla";

var forecasts:Array = str.split(".");

trace(forecasts.length);

var ind:int = 0;

var fores:Array = new Array("");

for (var i:int = 0; i< forecasts.length; i++){

                if(forecasts.indexOf(":") > -1){

                                ind += 1;

                                fores[ind] = (forecasts + ". ");

                } else {

                                fores[ind] += (forecasts + ". ");

                }

}

for(var j:int = 0; j<fores.length; j++){

                trace(j, fores);

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 25, 2018 Jul 25, 2018

Copy link to clipboard

Copied

Then what you can do would be similar but extended to process a bit more.  After you break the string into an array, you can then examine each element to see if it contains a colon ( : ) .  If it does not then you append it to the element before it.... build it into another array if it is easier for you.   I will leave you to getting it to work - it will.  If you happened to leave out more detail that would complicate it beyond that solution then think it thru.

Hint....  use the indexOf() method of the String class to check for the presence of the colon.

if (forecasts.indexOf(":") > -1){

     // there is a colon in the string

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines