本文介绍了如何检查字符串是否是数组中任何元素的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组
var months = ["January", "February", "March", "April", \
"May", "June", "July", "August", "September", "October", \
"November", "December"];
我有"Nov","October","Jun","June","Sept","Sep"等字符串.要点是,该字符串可以是一个月中的一个子字符串.
I have strings like "Nov", "October", "Jun", "June", "Sept", "Sep" etc. The point is, the string can be a substring of one of the months.
将字符串比较为数组元素的子字符串的最佳方法是什么?如何查找月份索引?
What is the best way to compare the string to be a substring of the array elements? How do I find out the index of the month?
我正在查看javascript/jQuery.
I am looking at javascript/jQuery.
我知道我可以使用search
遍历数组检查每个元素,并在找到时中断.我想要更好的东西.
I know I can just loop through the array checking against each element using search
and break when found. I want something better.
推荐答案
var month_index = function(target) {
target = target.toLocaleLowerCase();
return jQuery.inArray(true, jQuery.map(months, function(s) {
return s.toLocaleLowerCase().indexOf(target) > -1;
}))
};
var index_of_october = month_index("oct");
这篇关于如何检查字符串是否是数组中任何元素的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!