lastIndexOf Summary Gets the last position a substring occurs at or regular expression matches at. Signatures Click on a signature to select it and view its documentation. public int lastIndexOf(string searchFor) public int lastIndexOf(string searchFor, int endingIndex) public int lastIndexOf(System.RegExp searchFor) public int lastIndexOf(System.RegExp searchFor, int endingIndex) Usage public int lastIndexOf(string searchFor) public int lastIndexOf(string searchFor, int endingIndex) public int lastIndexOf(System.RegExp searchFor) public int lastIndexOf(System.RegExp searchFor, int endingIndex) Returns The position of the last occurrence of the substring. -1 if the substring was not found. The position of the last occurrence of the substring. This value will never exceed the ending index. -1 if the substring was not found. The last position the regular expression matches at. -1 if the regular expression failed to match anything. The last position the regular expression matches at. This value will never exceed the ending index. -1 if the regular expression failed to match anything. Parameters searchFor The substring to get the last index of. Parameters searchFor The substring to get the last index of. endingIndex The index to end searching for the substring at. Parameters searchFor The regular expression to match. Parameters searchFor The regular expression to match. endingIndex The index to end searching for the substring at. Description Gets the last position a substring occurs at. lastIndexOf will return -1 if the substring could not be found. Gets the last position a substring occurs at, up to a certain ending index. lastIndexOf will return -1 if the substring could not be found. Gets the last position a regular expression matches at. lastIndexOf will return -1 if the regular expression could not match. Gets the last position a regular expression matches at, up to a certain ending index. lastIndexOf will return -1 if the regular expression could not match. Examples Basic Usage 123456import System; string message = "The quick brown fox jumped again and again.";Console.log(message.indexOf("again")); // 27, first index of "again"Console.log(message.lastIndexOf("again")); // 37, last index of "again"Console.log(message.lastIndexOf("hound")); // -1, substring could not be found Basic Usage 123456import System; string message = "The quick brown fox jumped again and again.";Console.log(message.indexOf("again", 30)); // 27Console.log(message.lastIndexOf("again", 40)); // 37Console.log(message.lastIndexOf("hound", 30)); // -1 Basic Usage 123456import System; string message = "The quick brown fox jumped again and again.";Console.log(message.lastIndexOf(/AGAIN/i)); // 37Console.log(message.lastIndexOf(/again(?!\.)/)); // 27Console.log(message.lastIndexOf(/aaa/)); // -1 Basic Usage 1234567import System; string message = "The quick brown fox jumped again and again.";Console.log(message.lastIndexOf(/AGAIN/i, 30)); // 27Console.log(message.lastIndexOf(/AGAIN/i, 40)); // 37Console.log(message.lastIndexOf(/again(?!\.)/, 40)); // 27Console.log(message.lastIndexOf(/aaa/, 30)); // -1 Share HTML | BBCode | Direct Link