开发者

How to determine where's the place of number in a string

How to determine after which comma is the place of the number 13 in a string with commas(,) with JS?


For example: string - testtest,teststestatestb,testj The letter "s" is the 13-th letter and it's after the first comm开发者_如何学Pythona.


I'm not sure if I understand your Question. But here is a possible solution:

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);


You need to split string in array with ',' and then check for each string length using loop in correspond to array length. like

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.
  }
}


Try

a.indexOf('s',a.indexOf(','))

write a method

  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) )
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜