views:

49

answers:

3

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

A: 

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) )
   }
}
Salil
Yes , but the 13-th letter is undefined.I must find the 13-th letter first.
lam3r4370
A: 

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.
  }
}
Amit
+2  A: 

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);
jigfox
If I write 9 ,which is the first comma ,it returns 1 , but, for me ,it must be 0. I don't want to "count" the commas,but I can ignore this.Thank you very much!
lam3r4370
I've updated the function so it doesn't count the commas on `position`
jigfox
http://skype-icon.hit.bg/Icon/bow.gifhttp://www.funmansion.com/games/one_million_thank_yous.html
lam3r4370