问题描述
JS如何用逗号(,)确定字符串中数字13的位置在哪个逗号之后?
例如:string-testtest,teststestatestb,testj字母“ s”是第13个字母,在第一个逗号之后。
1楼
我不确定我是否理解您的问题。 但这是一个可能的解决方案:
function count_commas_to_position(string,position) {
return string.substring(0,position).replace(/[^,]/g,'').length
}
// if you don't want to count commas on `position`
function count_commas_to_position(string,position) {
return string.substring(0,position-1).replace(/[^,]/g,'').length
}
var string = "testtest,teststestatestb,testj"
var comma_count = count_commas_to_position(string,13);
2楼
尝试
a.indexOf('s',a.indexOf(','))
写一个方法
function abc(){
var a = "esttestte,ststestatestbtestj"
var c = a.indexOf(',')
if (c==-1)
alert("',' is not present ")
else{
var b =a.indexOf('s',c)
if (b==-1)
alert("'s' is not present after ','")
else
alert("position of 's' after ',' is "+(b+1) )
}
}
3楼
您需要使用','拆分数组中的字符串,然后使用对应于数组长度的循环检查每个字符串的长度。 喜欢
var str="testtest,teststestatestb,testj";
var a1 = new Array();
a1=str.split(",");
for(var i=0;i < a1.length ; i++ )
{
if(a1[i].length > 13)
{
//get 13th index of the word.
}
}