C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript String lastIndexOf() methodThe JavaScript string lastIndexOf() method is used to search the position of a particular character or string in a sequence of given char values. It behaves similar to indexOf() method with a difference that it start searching an element from the last position of the string. The lastIndexOf() method is case-sensitive. The index position of first character in a string is always starts with zero. If an element is not present in a string, it returns -1. SyntaxThese are the following ways used to search the position of an element.
Parametersch - It represent the single character to search like 'a'. index - It represent the index position from where search starts. str - It represent the string to search like "Java". JavaScript String lastIndexOf() method exampleLet's see the various ways of searching the position of an element with help of simple examples. Example 1Here, we will print the position of a single character. <script> var web="Learn JavaScript on TheDeveloperBlog"; document.write(web.lastIndexOf('a')); </script> Output: 23 Example 2In this example, we will provide the index value from where the search starts. <script> var web="Learn JavaScript on TheDeveloperBlog"; document.write(web.lastIndexOf('a',22)); </script> Output: 21 Example 3Here, we will print the position of a first character of string. <script> var web="Learn JavaScript on TheDeveloperBlog"; document.write(web.lastIndexOf("Java")); </script> Output: 20 Example 4In this example, we will provide the index value from where the search starts. <script> var web="Learn JavaScript on TheDeveloperBlog"; document.write(web.lastIndexOf("Java",19)); </script> Output: 6 Example 5Here, we will try to search the element by changing its case-sensitivity. <script> var web="Learn JavaScript on TheDeveloperBlog"; document.write(web.lastIndexOf("java")); </script> Output: -1
Next TopicJavaScript String
|