删除 AS3 中特定单词(字符串)后的所有内容
remove everything after a specific word (string) in AS3
我有这个字符串,我想通过删除特定单词之后和另一个特定单词开始时的所有内容,将它分成多个字符串。
示例文本:
"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 : blblablb"
我想为每个 "section" 创建多个字符串。上午弦乐部分,下午弦乐部分,夜间弦乐部分..等等
如果我们以我的早晨字符串为例:我如何删除 "This morning" 之前的所有内容以及 "this afternoon" 中的所有内容(为了只有,在我的早上字符串中:"This morning : the sun will be generous everywhere." )
所以,如果我是对的,我已经设法从 "This morning" select 做到了:
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);
但是我怎样才能添加或删除 "this afternoon" 中的所有内容?
(不能说"delete everything after "everywhere”因为"everywhere"这个词不会每次都写,这要看早上的天气。所以我需要在"this afternoon"出现)
编辑
他们可以在一个预测中包含多个句子"day"
编辑 2
我从未使用过正则表达式。如果有人能给我一个例子,说明如何 select 成为我文本中的一部分,我将不胜感激。
如果预测始终具有相同的结构 ("This morning : ... This afternoon : ... This night : ... Tomorrow morning : ..."),您可以安全地使用这些短语来从中拆分部分。
我并不是说这是最好或最有效的方法,但如果您的预测具有这种一致性,它应该会起作用。
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 : blblablb";
var separators:Array = new Array("This morning :","This afternoon :","This night :","Tomorrow morning :");
function separate(forecast:String):Object{
var obj:Object = new Object();
obj.pre = forecast.split(separators[0])[0];
obj.morning = forecast.split(separators[0])[1].split(separators[1])[0];
obj.afternoon = forecast.split(separators[1])[1].split(separators[2])[0];
obj.night = forecast.split(separators[2])[1].split(separators[3])[0];
obj.tomorrow = forecast.split(separators[3])[1];
return obj;
}
var forecastObj:Object = separate(str);
trace(forecastObj.pre);
trace("This morning :"+forecastObj.morning);
trace("This afternoon :"+forecastObj.afternoon);
trace("This night :"+forecastObj.night);
trace("Tomorrow morning :"+forecastObj.tomorrow);
我有这个字符串,我想通过删除特定单词之后和另一个特定单词开始时的所有内容,将它分成多个字符串。
示例文本:
"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 : blblablb"
我想为每个 "section" 创建多个字符串。上午弦乐部分,下午弦乐部分,夜间弦乐部分..等等
如果我们以我的早晨字符串为例:我如何删除 "This morning" 之前的所有内容以及 "this afternoon" 中的所有内容(为了只有,在我的早上字符串中:"This morning : the sun will be generous everywhere." )
所以,如果我是对的,我已经设法从 "This morning" select 做到了:
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);
但是我怎样才能添加或删除 "this afternoon" 中的所有内容?
(不能说"delete everything after "everywhere”因为"everywhere"这个词不会每次都写,这要看早上的天气。所以我需要在"this afternoon"出现)
编辑
他们可以在一个预测中包含多个句子"day"
编辑 2
我从未使用过正则表达式。如果有人能给我一个例子,说明如何 select 成为我文本中的一部分,我将不胜感激。
如果预测始终具有相同的结构 ("This morning : ... This afternoon : ... This night : ... Tomorrow morning : ..."),您可以安全地使用这些短语来从中拆分部分。
我并不是说这是最好或最有效的方法,但如果您的预测具有这种一致性,它应该会起作用。
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 : blblablb";
var separators:Array = new Array("This morning :","This afternoon :","This night :","Tomorrow morning :");
function separate(forecast:String):Object{
var obj:Object = new Object();
obj.pre = forecast.split(separators[0])[0];
obj.morning = forecast.split(separators[0])[1].split(separators[1])[0];
obj.afternoon = forecast.split(separators[1])[1].split(separators[2])[0];
obj.night = forecast.split(separators[2])[1].split(separators[3])[0];
obj.tomorrow = forecast.split(separators[3])[1];
return obj;
}
var forecastObj:Object = separate(str);
trace(forecastObj.pre);
trace("This morning :"+forecastObj.morning);
trace("This afternoon :"+forecastObj.afternoon);
trace("This night :"+forecastObj.night);
trace("Tomorrow morning :"+forecastObj.tomorrow);