Created
April 20, 2014 18:27
-
-
Save xxl007/11121233 to your computer and use it in GitHub Desktop.
Finding a Substring in a String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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