从Javascript中的字符串中获取最后一个单词 [英] Getting the last word from a string in Javascript
本文介绍了从Javascript中的字符串中获取最后一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们如何使用JavaScript / jQuery从字符串中获取最后一个单词?
How can we get the last word from a string using JavaScript / jQuery?
在下面的场景中,最后一个单词是Collar。单词用 - 分隔。
In the following scenario the last word is "Collar". The words are separated by "-".
Closed-Flat-Knit-Collar
Flat-Woven-Collar
Fabric-Collar
Fabric-Closed-Flat-Knit-Collar
推荐答案
为什么一切都必须在jQuery中?
Why must everything be in jQuery?
var lastword = yourString.split("-").pop();
这会将您的字符串拆分为单个组件(例如,已关闭
, Flat
, Knit
, Collar
)。然后它将弹出数组的最后一个元素并返回它。在您提供的所有示例中,这是 Collar
。
This will split your string into the individual components (for exampe, Closed
, Flat
, Knit
, Collar
). Then it will pop off the last element of the array and return it. In all of the examples you gave, this is Collar
.
这篇关于从Javascript中的字符串中获取最后一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文