1
我在閃光燈有一個文本字段,它包含以下字符串刪除最後一個單詞:如何從一個字符串
txtFld.text = " Mr. Suresh Kumar has written this article"
現在,我想要做的是,我想刪除的最後一個字出來的這和看起來像:
txtFld.text = " Mr. Suresh Kumar has written this"
請幫幫忙,謝謝
我在閃光燈有一個文本字段,它包含以下字符串刪除最後一個單詞:如何從一個字符串
txtFld.text = " Mr. Suresh Kumar has written this article"
現在,我想要做的是,我想刪除的最後一個字出來的這和看起來像:
txtFld.text = " Mr. Suresh Kumar has written this"
請幫幫忙,謝謝
可以使用的.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沒有正則表達式的支持,你應該確保輸入預先修剪(從前端和末端空格去掉)。
試試這個:
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
非常好,單行! – Cherniv
@Cherniv謝謝 - 儘管你的方法更常見。 – Marty