Javascript lastindexof in string

JavaScript String lastIndexOf()

Summary: in this tutorial, you’ll learn how to use the JavaScript String lastIndexOf() method to locate the last occurrence of a substring in a string.

Introduction to the JavaScript String lastIndexOf() Method

The String.prototype.lastIndexOf() returns the last occurrence of a substring ( substr ) in a string ( str ).

str.lastIndexOf(substr, [, fromIndex]);Code language: JavaScript (javascript)

It returns -1 if the str does not contain the substr .

The lastIndexOf() method searches for the substring backward from the fromIndex . The fromIndex is optional and defaults to +Infinity . It means that if you omit the fromIndex , the search starts from the end of the string.

If the fromIndex is greater or equal to str.length , the lastIndexOf() will search for the substr in the whole string.

If the fromIndex is less than zero, the search behavior is the same as if the fromIndex were zero.

The lastIndexOf() always perform a case-sensitive search.

To find the index of the first occurrence of a substring within a string, you use the last indexOf() method.

JavaScript String lastIndexOf() examples

Let’s take some examples of using the lastIndexOf() method.

1) Using lastIndexOf() method

This example uses the lastIndexOf() method to locate the last occurrence of the substring ‘a’ in the string ‘JavaScript’ :

let str = 'JavaScript'; let index = str.lastIndexOf('a'); console.log(index);Code language: JavaScript (javascript)
3Code language: JavaScript (javascript)

If you pass the fromIndex argument to the string, the lastIndexOf() method will start searching backward from the fromIndex as shown in the following example:

let str = 'JavaScript'; let index = str.lastIndexOf('a',2); console.log(index);Code language: JavaScript (javascript)
1Code language: JavaScript (javascript)

2) The lastIndexOf() and case-sensitivity

The lastIndexOf() is case-sensitive. The following example returns -1:

let str = 'Hello, World!'; let substr = 'L'; let index = str.lastIndexOf(substr); console.log(index); // -1Code language: JavaScript (javascript)

To perform a case-insensitive search for the index of the last occurrence of a substring within a string, you can convert both substring and string to lowercase before applying the lastIndexOf() method as follows:

let str = 'Hello, World!'; let substr = 'L'; let index = str.toLocaleLowerCase().lastIndexOf(substr.toLocaleLowerCase()); console.log(index); // -1 Code language: JavaScript (javascript)

Summary

  • The lastIndexOf() returns the index of the last occurrence of a substring in a string, or -1 if the string does not contain the substring. It searches for the substring backward from the end of the string or from the fromIndex if this argument is available.
  • The lastIndexOf() carries a case-sensitive search.
Читайте также:  Python get list files folder

Источник

JavaScript String lastIndexOf()

The lastIndexOf() method returns the index (position) of the last occurrence of a specified value in a string.

The lastIndexOf() method searches the string from the end to the beginning.

The lastIndexOf() method returns the index from the beginning (position 0).

The lastIndexOf() method returns -1 if the value is not found.

The lastIndexOf() method is case sensitive.

See Also:

Syntax

Parameters

Parameter Description
searchvalue Required.
The string to search for.
start Optional.
The position where to start.
Default value is string length.

Return Value

More Examples

Search for the last occurrence of «planet», starting at position 20:

let text = «Hello planet earth, you are a great planet.»;
let result = text.lastIndexOf(«planet», 20);

Browser Support

lastIndexOf() is an ECMAScript1 (ES1) feature.

ES1 (JavaScript 1997) is fully supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes

Источник

JavaScript lastIndexOf() Method — Check Last Occurrence Of A String

In this tutorial, you will learn about the lastIndexOf() method in JavaScript and will know how to get the last occurrence of any string within a string.

last indexof method

JavaScript lastIndexOf() String Method

The lastIndexOf() is used to find the last occurrence of a substring within another calling string.

It returns the last index value of the string found within the calling string. If no such string is present in the calling string then it returns -1 .

Note — For the efficiency of the method, the method starts checking from the end of the string and returns the first occurrence index from backward.

var string = "learning to code is learning to create and innovate"; var search_str = "learning"; // return last occurance of word "learning" console.log(string.lastIndexOf(search_str)); // 20 console.log(string.lastIndexOf("king")); // -1

You can see in the above example the method returns the index of the last occurrence of the search string («learning») which is 20 although it first occurs at index 0. Also when searching for the word «king» (which is not in the calling string) it returns -1.

Note : Even though the lastIndexOf() method searches the string from backward, it still returns the location value of the string’s first letter from the end of the string.

Syntax of lastIndexOf method

The syntax of javascript lastIndexOf() string method is:

string.lastIndexOf(searchValue, position);

javascript lastIndexOf Method

  1. searchValue — This is the string or character to be searched within the given string.
  2. position ( optional ) — It is an optional argument that specifies that the search should be searched before or equal to the given position. If the position is not specified, then the length of the string.
Читайте также:  Pair in java example

Since the method starts from the backward of the string so if you define the position to be 10, then it will start checking for the value from 10 to 0.

If position >= string.length then the whole string is searched. If position > 0 it would search for nothing.

const string = "to do or not to do"; console.log(string.lastIndexOf("do", 10)); // 3 console.log(string.lastIndexOf("do")); // 16 console.log(string.lastIndexOf("or", 8)); // 6 console.log(string.lastIndexOf("or")); // 6

Note : ‘tobetobe’.lastIndexOf(‘to’, 4) will return 4 and not 0, as search_from limits only the beginning of the match.

Case-sensitivity

The lastIndexOf() method is case sensitive.

See the following example:

const str = "To do or not to do"; console.log(str.lastIndexOf("To")); // 0 console.log(str.lastIndexOf("to")); // 13

Using indexOf() and lastIndexOf() Method

The indexOf() method is similar to the lastIndexOf method but it returns the first matching string rather than the last.

Here is an example that searches for the same word using 2 different methods.

const str = "To do or not to do"; console.log(str.indexOf("do")); // 3 console.log(str.lastIndexOf("do")); // 16

Conclusion

The lastIndexOf() method finds and returns the last index of the specified substring from the given string. It is case-sensitive. To get the first index of a specified string using the indexOf() method.

Источник

lastIndexOf() in Javascript

Javascript Course - Mastering the Fundamentals

The lastIndexOf method in javascript returns the index of the last occurrence of the specified substring. Given a second argument, a number, the lastIndexOf() method returns the last occurrence of the specified substring at an index less than or equal to the number.
The lastIndexOf() method returns -1 if the argument string is not found in the base string.

Syntax of String lastIndexOf() in Javascript

The following is the syntax of the lastIndexOf() in Javascript:

Parameters of String lastIndexOf() in Javascript

  • stringToSearch : The string to be searched in the base string is stringToSearch.
    For example, if we have to find the last occurrence of «JS» in the string «JS IS A SCRIPTING LANGUAGE» . Here «JS» is the stringToSearch and the base string is «JS IS A SCRIPTING LANGUAGE» .
  • position : The lastIndexOf in javascript returns the index of the last occurrence of the argument string, here stringToSearch , at an index less than or equal to the position. By default position’s value is infinity.

Return Value of String lastIndexOf() in Javascript

Return type: number

The lastIndexOf in javascript returns the 0-based index of the last occurrence of stringToSearch or returns -1 if the index is not found.

Читайте также:  Генерация коротких ссылок php

What is String lastIndexOf() in Javascript?

The String.lastIndexOf() method in JavaScript searches the string from the end to the beginning of the string. If this method founds an occurrence of the argument provided, the lastIndexOf() method stops the searching and returns the 0-based index of the argument string. If the argument string is not present, then this method returns -1.

But note that, the lastIndexOf() in javascript returns the index starting from the beginning of the string.

What is lastIndexOf in javascript

As you can see in the above image. String «JS» is being searched in the string «JS IS A SCRIPTING LANGUAGE«.

Case-sensitivity

The lastIndexOf() in javascript is case-sensitive. Therefore «javascript» and «JavaScript» are two different strings for this method.

Example of String lastIndexOf() Method in JavaScript

Let’s have an example to understand our learnings.

The lastIndexOf in javascript is case-sensitive. Therefore «javascript» and «JavaScript» are two different strings for this method. Let’s have an example for this case:

This time the lastIndexOf() method returned -1 , as this method treats » javascript » and » JavaScript » differently.

Using indexOf() and lastIndexOf()

Suppose we have to find the first and last index of a string in another base string. We will use the indexOf() method and the lastIndexOf() method to obtain the respective indexes.

A String is a 0-based indexed data type , this means that the first character of a string is present at index 0 .

For example: Consider the string «APPLE«. The first character of this string that is «a» is present at index 0.

Now, let’s have a base string » THIS IS A BASE STRING «.

last position of the first character of string

So, now we have to return the last index of character «A«. In the above image, we can see the last index of character «A» is 11. Therefore we will return 11.
To find the last position of the first character of a string, we can use the lastIndexOf() method. Instead of passing the stringToSearch , we will pass the first character of stringToSearch, which is stringToSearch[0] . Let’s look over the example to understand this:

The last position of the character «a» in the base string is 11, therefore lastIndexOf() method returns 11 .

Conclusion

  • The String.lastIndexOf() method in JavaScript returns the last index of the argument string in the base string.
  • If the argument string is not found in the base string then, lastIndexOf() method returns -1 .
  • The lastIndexOf() method has two parameters, stringToSearch and position (optional).
  • The indexOf() method and lastIndexOf() method is used to find the first and the last occurrence of a string in a base string.

Источник

Оцените статью