Find first index of a string after index

Javascript

Javascript Problem Overview


I have a string: "www.google.com.sdg.jfh.sd"

I want to find the first ".s" string that is found after "sdg".

so I have the index of "sdg", by:

var start_index = str.indexOf("sdg");

now I need to find the first ".s" index that is found after "sdg"

any help appreciated!

Javascript Solutions


Solution 1 - Javascript

There's a second parameter which controls the starting position of search:

String.prototype.indexOf(arg, startPosition);

So you can do

str.indexOf('s', start_index);

Solution 2 - Javascript

This code might be helpful

var string = "www.google.com.sdg.jfh.sd",
  preString = "sdg",
  searchString = ".s",
  preIndex = string.indexOf(preString),
  searchIndex = preIndex + string.substring(preIndex).indexOf(searchString);

You can test it HERE

Solution 3 - Javascript

var str = "www.google.com.sdg.jfh.sd";
var search = "sdg";
var start_index = str.substring(str.indexOf(search) + search.length).indexOf(".s");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlon ShmielView Question on Stackoverflow
Solution 1 - Javascriptlukas.pukenisView Answer on Stackoverflow
Solution 2 - JavascriptmatewkaView Answer on Stackoverflow
Solution 3 - JavascriptjasonslyviaView Answer on Stackoverflow