如何从字符串中删除最后一个单词

时间:2013-08-06 05:53:02

标签: flash actionscript actionscript-2 flash-cs5

我在flash中有一个文本字段,其中包含以下字符串:

txtFld.text = " Mr. Suresh Kumar has written this article"

现在,我想要做的是,我想删除最后一个字,看起来像:

txtFld.text = " Mr. Suresh Kumar has written this"

请帮助, 感谢

2 个答案:

答案 0 :(得分:2)

试试这个:

    var text = txtFld.text;   // Saving text field' value in temporary variable
    text = text.split(" ");   // Splitting it at space delimiter
    text.splice(text.length-1 , 1); // Throwing out the last word
    txtFld.text = text.join(" ");   // Concatenating whole thing back

答案 1 :(得分:2)

您可以结合使用.slice().lastIndexOf()

var base:String = "Mr. Suresh Kumar has written this article";

// Slice up until the last whitespace character.
var trunc:String = base.slice(0, base.lastIndexOf(" "));
trace(trunc);

因为AS2没有正则表达式支持,所以你应该确保预先修剪输入(从正面和结尾删除空格)。