1

I have a JavaScript variable which contains something like "fld_34_46_name". I need to be able to find the location of the THIRD _. The numbers, and name are not always the same (so, the string might also look like "fld_545425_9075_different name_test").

Is this possible? How could I do it?

Felix Kling
  • 756,363
  • 169
  • 1,062
  • 1,111
user2370460
  • 6,960
  • 10
  • 29
  • 42

1 Answers1

4

Use the indexOf method three times:

var i = s.indexOf('_');
i = s.indexOf('_', i + 1);
i = s.indexOf('_', i + 1);

Note: If the string might contain fewer than three underscores, you would need to check for -1 after each time.

NullUserException
  • 81,190
  • 27
  • 202
  • 228
Guffa
  • 666,277
  • 106
  • 705
  • 986