我在 Flash 中有一个文本字段,其中包含以下字符串:
txtFld.text = " Mr. Suresh Kumar has written this article"
现在,我想做的是,我想从中删除最后一个词,看起来像:
txtFld.text = " Mr. Suresh Kumar has written this"
请帮忙,谢谢
我在 Flash 中有一个文本字段,其中包含以下字符串:
txtFld.text = " Mr. Suresh Kumar has written this article"
现在,我想做的是,我想从中删除最后一个词,看起来像:
txtFld.text = " Mr. Suresh Kumar has written this"
请帮忙,谢谢
尝试这个:
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
您可以使用.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 不支持正则表达式,所以您应该确保预先修剪输入(从前面和结尾删除空格)。