Skip to content

Instantly share code, notes, and snippets.

@xxl007
Created April 20, 2014 18:27
Show Gist options
  • Save xxl007/11121233 to your computer and use it in GitHub Desktop.
Save xxl007/11121233 to your computer and use it in GitHub Desktop.
Finding a Substring in a String
// Use the String object's built-in indexOf method to find the position of the substring if it exists:
var testValue = "This is Cookbook's test string";
var subsValue = "Cookbook";
var iValue = testValue.indexOf(subsValue, 5);
// returns index position of first character of substring, (would have returned -1 if not found)
// second parameter indicates index where search is to be started
//alternative
var iValue = testValue.lastIndexOf(subvalue, 3);
// same but in the opposite index direction (right to left)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment