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
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
...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
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
Copy link to clipboard
Copied
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
}
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
}